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