ASP.Net 2.0 – How to use the new ValidationGroup property

With the release of ASP.Net 2.0 came a host of Validation features. In this series I'll be touching on those and show you how easily it now is to create validation for your forms.

In part 1 i'll be showing a new feature/property of existing validation tools – namely the ValidationGroup property. With this property you can now gather section specific validation together, so now there's no need to assess what to validate on PostBack.

In forms where you have several sections – some which might be dependant on others – some which might be individual and not be required. You can still validate these very easily should somebody input data into the fields.

For the purpose of illustrating this feature i'll be creating a very simple login form (not using any of the Login controls in ASP.Net 2.0).

 ValidationGroup - Login Form

as you can see, i've used the following controls here:

  1. ValidationSummary
  2. Label – Username
  3. Label – Password
  4. TextBox – Username
  5. TextBox – Password
  6. Button – Login
  7. RequiredFieldValidator – Username
  8. RequiredFieldValidator – Password

To group them all together in the same ValidationGroup you need to set the group name in the ValidationGroup property, which can be found at the very bottom of the property list if you sort it alphabetically.

I chose to use LoginForm as the ValidationGroup name. This will have to be done for each control you want to be grouped together.

ValidationGroup property - TextBox ValidationGroup property - password 

ValidationGroup property - Button ValidationGroup property - ValidationSummary

ValidationGroup property - RequiredFieldValidator ValidationGroup property - RequiredFieldValidator 2

Now on each of the RequiredFieldValidator controls i'll set the ControlToValidate and Display property.

ControlToValidate property

Display property

Now the reason for selecting None in the Display property is that i want the ErrorMessage value to display in the ValidationSummary.

Simple as that – you've now grouped the fields together with the ValidationSummary and both RequiredFieldValidator controls and can have several grouped validation rules in place on the same form.

ValidationGroup property - ResetForm ValidationGroup property - ResetForm

7 thoughts on “ASP.Net 2.0 – How to use the new ValidationGroup property”

  1. I am currently getting a viewstate error every time I try to submit either form on a page.

    Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

    I am going to try some different things.

  2. yeah – lost all the images from the server a while ago.

    sorry about that guys…

    Swati, what exactly is the problem you”re having?

Comments are closed.