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 Orlando last month: http://www.scottgu.com/whidbey_tipsandtricks.zip.  The “02_ValidationGroup” subdirectory shows a simple search example that illustrates this scenario.

 

All in all a pretty small new feature in ASP.NET Whidbey – but hopefully a pretty useful one for people building apps today.

7 Comments

  • Will it be possible to have a validation control belong to more than one group?



    eg, an input form for a mapping application:



    Address

    City

    State

    ZIP



    Group1 -&gt; (Address, City, State)

    Group2 -&gt; (Address, ZIP)

    Group3 -&gt; (City, State)

    Group4 -&gt; (ZIP)



    If not, then a shared validation criterion would have to be duplicated

  • You're awesome Scott!

  • You done it again :p

  • Why do all your examples and source in your books use &ldquo; AND &rdquo; instead of &quot; &quot; ? It prevents easy cut and pasting of code and is very annoying?
    Similarly if using line numbers for code why not put it in a seperate column so JUST source can be selected and copied/pasted.
    Considering YOU are teaching people I find it alarming that such fundamental, obvious, basic annoyances are overlooked. I can just see a newbie copying and pasting and getting errors on &nbsp;&ldquo; and &rdquo; or line numbers and just giving up...
    Otherwise well done

  • Get a life Hal Burton. The guys trying to help people and your moaning.

    If you can think of an improvement you could point it out more politely!

  • My page have many groups and i want to check just 1 times when user pushes a button.Is there any technique to solve my problem?

  • I see these groups of great utility for AJAX.

Comments have been disabled for this content.