Validation Groups Re-Explained, including Multiple Groups

I got a question about a blog post with using multiple ValidationSummary controls and multiple validation groups, and I think my original post is unclear.  Suppose you have a form that uses two validation groups: Account and Profile.  In order to validate the account portion of the form, ensure all  TextBoxes, Validation controls, and the ValidationSummary has this value supplied, and the same goes for the profile. This would look like:

<asp:ValidationSummary id="vs" runat="server" ValidationGroup="Account" />

<asp:TextBox id="user" runat="server" ValidationGroup="Account" />
<asp:requiredfieldvalidator id="userval" runat="server" ValidationGroup="Account" .. />
.
.

So all of the validators are linked to this validation summary.  Unfortunately, the ValidationSummary control only appears for validators within the same validation group.  Note that specifying no validation group is also a value, and only validators with no validation summary get validated.  So this would validate together correctly.

<asp:ValidationSummary id="vs" runat="server" />

<asp:TextBox id="user" runat="server" />
<asp:requiredfieldvalidator id="userval" runat="server" .. />

But anything within the Account val group won't appear in this validation summary.  So the ValidationSummary control is limited this way, an unfortunate aspect of the control.

Now, if there are multiple validation groups, that works OK.  To trigger validation on the back-end, you can use:

Page.Validate("Account");

To validate that validation group.  Use no parameters to validate all validation groups, and use:

Page.Validate(null);

To validate only the default validation group, where no ValidationGroup property is supplied.  A button can also trigger this by defining it's validation group, and not specifying the CausesValidation="false" declaration as such:

<asp:Button id="btnSave" runat="server" Text="Save" ValidationGroup="Account" />

Published Wednesday, February 11, 2009 8:23 PM by bmains
Filed under:

Comments

# Link Listing - February 11, 2009

Wednesday, February 11, 2009 10:34 PM by Christopher Steen

Link Listing - February 11, 2009

# Link Listing - February 11, 2009

Wednesday, February 11, 2009 10:35 PM by Christopher Steen

ASP.NET Encrypting or Decrypting the Configuration File [Via: bmains ] Validation Groups Re-Explained,...