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.

Published Sunday, October 24, 2004 3:53 PM by ScottGu

Comments

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Monday, October 25, 2004 2:27 AM by Fredrik Normén
For you who don't know what cross-page postback is, can take a look at this post on my blog:

http://fredrik.nsquared2.com/viewpost.aspx?PostID=175

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Monday, October 25, 2004 2:30 PM by Keith J. Farmer
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 -> (Address, City, State)
Group2 -> (Address, ZIP)
Group3 -> (City, State)
Group4 -> (ZIP)

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

# Simulating ValidationGroup (of ASP.NET 2.0) feature in ASP.NET 1.X

Monday, December 06, 2004 2:18 AM by TrackBack

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Tuesday, June 13, 2006 8:34 AM by Nila
You're awesome Scott!

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Monday, July 31, 2006 9:39 AM by Abbad Minhas
You done it again :p

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Wednesday, November 22, 2006 8:40 PM by Hal Burton

Why do all your examples and source in your books use “ AND ” instead of " " ? 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  “ and ” or line numbers and just giving up...

Otherwise well done

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Wednesday, January 03, 2007 6:33 AM by Lucas Tarranteos

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!

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Wednesday, January 17, 2007 10:40 PM by Lenh Ho Xung

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?

# re: ASP.NET Whidbey Tip and Trick: Validation Groups

Thursday, March 08, 2007 12:27 AM by intrader

I see these groups of great utility for AJAX.