The problem is described in this post, basically it can be summarized in “you have a user control with validators, you put more than one instance of the user control in a page, all validators are fired together”
The above post already gives a solution, but is not a general one. I want to avoid the need to go into the user control and tweak with validators, so I came up with this little solution
public static void AssignValidationGroup(Control rootControl) { AssignValidationGroup(rootControl, rootControl.ID); } private static void AssignValidationGroup(Control control, String rootName) { if (control is BaseValidator) { ((BaseValidator) control).ValidationGroup = "vg_" + rootName; } else if (control is Button) { Button thebtn = (Button) control; thebtn.ValidationGroup = "vg_" + rootName; } else if (control is ValidationSummary) { ValidationSummary theSummary = (ValidationSummary)control; theSummary.ValidationGroup = "vg_" + rootName; } foreach (Control child in control.Controls) { AssignValidationGroup(child, rootName); } }
The function AssignValidationGroup accepts a Control, then it iterates recursively into inner controls, and for each control that descends from BaseValidator, it assign a group name based on the id of the root control. Now in the load event of a page that instantiate more than one instance of a user control with validator I can write.
WebUtils.AssignValidationGroup(MyUserControl1)
WebUtils.AssignValidationGroup(MyUserControl2)
This automatically assign different validation group to both the user control.
alk.
Tags: asp.net
Tags: ASP.NET






Leave a Reply