public class HtmlValidationBasePage : Page
{ protected HtmlValidationBasePage()
{
}
protected override void OnLoad(EventArgs e)
{ List<Control> controls = new List<Control>();
FindControls<Control>(this.Page.Controls, controls);
if (controls.Count > 0)
{ controls.ForEach(delegate(Control control)
{ IEditableTextControl textControl = control as IEditableTextControl;
if (textControl != null)
{ HtmlInputValidator handler = new HtmlInputValidator();
handler.ControlToValidate = control.UniqueID;
handler.Display = ValidatorDisplay.Dynamic;
handler.Text = "Failed Validation for control " + control.ID;
handler.ErrorMessage = "Failed Validation for control " + control.ID;
handler.SetFocusOnError = true;
handler.EnableClientScript = false;
handler.ID = control.ID + "Validator";
control.Controls.Add(handler);
}
});
}
ValidationSummary summary = new ValidationSummary();
summary.ShowSummary = true;
summary.DisplayMode = ValidationSummaryDisplayMode.List;
Page.Form.Controls.Add(summary);
base.OnLoad(e);
}
// Recurse through all of the controls on the page
protected T FindControls<T>(ControlCollection controls, List<T> foundControls) where T : Control
{ T found = default(T);
if (controls != null && controls.Count > 0)
{ for (int i = 0; i < controls.Count; i++)
{ found = controls[i] as T;
if (found != null)
{ foundControls.Add(found);
}
found = FindControls<T>(controls[i].Controls, foundControls);
}
}
return found;
}
}