How to set the ValidationGroup dynamically

asp.netvalidationvalidationsummary

I have a ASP.NET 2.0 webpage with 2 UserControls (.ascx). Each UserControl contains a bunch of validators. Placing a ValidationSummary on the page will display all validation errors, of both UserControl's. Placing a ValidationSummary in each UserControl will display all the errors of both controls twice.

What I want is a ValidationSummary for each UserControl, displaying only the errors on that UserControl.

I've tried to solve this by setting the ValidationGroup property of the validators on each usercontrol dynamicaly. That way each validationsummary should display only the errors of its UserControl. I've used this code:

foreach (Control ctrl in this.Controls)
{
    if (ctrl is BaseValidator)
    {
        (ctrl as BaseValidator).ValidationGroup = this.ClientID;
    }
}
ValidationSummary1.ValidationGroup = this.ClientID;

This however seems to disable both clientside and server side validation, because no validation occurs when submitting the form.

Help?

Best Answer

The control that is causing your form submission (i.e. a Button control) has to be a part of the same validation group as any ValidationSummary and *Validator controls.