locked
turn off CausesValidation for all buttons on a web page RRS feed

  • Question

  • User-125499312 posted

    how would <g class="gr_ gr_11 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="11" data-gr-id="11">i</g> turn off CausesValidation for all buttons on a web page?

    thx

    Wednesday, January 2, 2019 5:56 PM

All replies

  • User-943250815 posted

    Since you do not want Validation, why not remove or comment all Validators.
    The alternative could be change property on Code Behind, but it will take some work anyway

    Wednesday, January 2, 2019 7:42 PM
  • User36583972 posted


    Hi yzidell,

    Button.CausesValidation: By default, page validation is performed when a Button control is clicked. Page validation determines whether the input controls associated with a validation control on the page all pass the validation rules specified by the validation control.

    To prevent validation from being performed, set the CausesValidation property to false.

    how would i turn off CausesValidation for all buttons on a web page?

    You can try to find all buttons on a page and set the CausesValidation property to false.

       if(!IsPostBack)
                {
                    List<Button> allControls = new List<Button>();
                    GetControlList<Button>(Page.Controls, allControls);
                    foreach (var childControl in allControls)
                    {
                        childControl.CausesValidation = false;
    
                        //     call for all controls of the page
                    }
                }
    
         private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
    where T : Control
            {
                foreach (Control control in controlCollection)
                {
                    //if (control.GetType() == typeof(T))
                    if (control is T) // This is cleaner
                        resultCollection.Add((T)control);
    
                    if (control.HasControls())
                        GetControlList(control.Controls, resultCollection);
                }
            }

    Best Regards,

    Yong Lu

    Thursday, January 3, 2019 5:53 AM