ASP.NET Whidbey Tip and Trick: Validation Groups
A small but cool new feature of ASP.NET Whidbey is its ValidationGroup support on validation and postback controls.
In ASP.NET V1 and V1.1 control validation occurs in an all or nothing kind of way. If you have two textboxes that each have a validation control applied against it, and two buttons on the form, both validation controls will always check for validation together – there is no way to cause one of the validation controls to fire when one button is clicked, and the other to fire when the other button is clicked. Note that you can disable validation altogether when the button is clicked (by setting the “CausesValidation” property on the button) – what is missing is the ability to-do granular validation.
ASP.NET V2 introduces a new “ValidationGroup” property on validation and input controls that now makes this possible. This allows page developers to group different controls together for more granular validation behavior.
Using the ValidationGroup property is simple – just add a “ValidationGroup” property to the validation controls that you want to group together, and then add the same ValidationGroup name to the postback control (for example: a button) that you want to cause the validation to occur.
For example, the trivial sample below demonstrates two groups – a “Group1” and a “Group2” of validators. There are then two buttons on the page – when button1 is clicked, the first group of validators will fire. When button2 is clicked, the second group of validators will fire. Postback will be blocked client-side by default if the validation fails:
<html>
<body>
<form runat=“server”>
<asp:textbox id=“TextBox1” runat=“server”/>
<asp:requiredfieldvalidator ValidationGroup=“Group1”
ErrorText=“Need to Fill in Value!”
ControlToValidate=“TextBox1”
runat=“server”/>
<asp:textbox id=“TextBox2” runat=“server”/>
<asp:requiredfieldvalidator ValidationGroup=“Group2”
ErrorText=“Need to Fill in Value!”
ControlToValidate=“TextBox2”
runat=“server”/>
<asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/>
<asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/>
</form>
</body>
</html>
On the server-side, developers can also now check whether a validation sub-group is valid by using the new overloaded Page.Validate(“groupname”) method.
The validationgroup feature becomes super-useful when combined with cross-page postbacks. A scenario to illustrate this would be when you have a search textbox and button on the top of your page, and you want to post this directly to a search.aspx page without having to-do a postback to the same page and then a manual re-direct. ValidationGroups allow developers in this scenario to require validation of the search textbox prior to posting to the search.aspx page – while cleanly partitioning this validation logic from the rest of the input on other places of the page.
For a more complete example of this scenario, you can download my tips/tricks talk from VSLive in
All in all a pretty small new feature in ASP.NET Whidbey – but hopefully a pretty useful one for people building apps today.