Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Group Validators in ASP.NET 2.0


Getting the data right requires you to apply a validation step to any external input. In ASP.NET, validation controls provide an easy-to-use mechanism to perform a variety of validation tasks, including testing for valid types, values within a given range, or required fields. Validation controls are server controls that perform a particular task on the input of a given input control in the same page. ASP.NET defines validators to check a field for nullness, against a particular range of values or a regular expression. The association between the validator and the monitored control is purely declarative and set mostly at design time.

All validators defined on a page are automatically grouped in the Validators collection of the Page class. You can validate input controls all in a single shot using the Validate method in the Page class or individually by calling the Validate method on individual validator controls. The Validate method sets the IsValid property both on the page and on the individual validator. The IsValid property indicates whether the user’s entries match the requirements of the validators. Other than explicitly using the Validate method, the user’s entry is also automatically validated whenever the page posts back.

In ASP.NET 1.x, control validation occurs in an all-or-nothing kind of way. For example, if you have a set of input and validation controls and two buttons on the form, clicking either button will always validate all controls. In other words, there’s no way to validate some controls when one button is clicked, and some others when another button is clicked. The CausesValidation property on button controls allows you to disable validation on a button, but that is another story. What is missing is the ability to do validation on a group of controls. This is just what the ValidationGroup property provides in ASP.NET 2.0. The property is available on validators, input, and button controls.

Using the ValidationGroup property is simple: Just define it for all the validation controls that you want to group together, and then assign the same name to the ValidationGroup property of the button that you want to fire the validation.

<asp:textbox runat="server" id="TextBox1"  />
<asp:RequiredFieldValidator runat="server"  
    ValidationGroup="Group1"
    ControlToValidate="TextBox1" 
    ErrorMessage="TextBox1 is mandatory" />
<asp:textbox runat="server" id="TextBox2"  />
<asp:RequiredFieldValidator runat="server"   
    ValidationGroup="Group2"
    ControlToValidate="TextBox2" 
    ErrorMessage="TextBox2 is mandatory" />
<asp:Button runat="server" Text="Group1" ValidationGroup="Group1" />
<asp:Button runat="server" Text="Group2" ValidationGroup="Group2" />

The two RequiredFieldValidator controls belong to distinct validation groups—Group1 and Group2. The first button validates only the controls defined within Group1; the second button takes care of the input associated with Group2. In this way, the validation process can be made as granular as needed.

The validation group feature gets especially helpful when combined with cross-page postbacks—a new ASP.NET 2.0 feature. Cross-page postback allows a button to post the contents of the current form to another page in a certain way, overriding the single-form model of ASP.NET. Imagine you have a search box in your page, and you want to post its contents directly to a search page without passing through the classic postback mechanism and an additional redirect. Validation groups allow you to check only the contents of the search textbox prior to posting to the search page.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.