O'Reilly logo

ASP.NET 2.0 Cookbook, 2nd Edition by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.7. Using Validation Groups to Support Login and New User Registration Within a Single Form

Problem

You have two logical sections on a form with controls that require different validation as a function of the button the user clicks, such as a form that supports login and new user registration.

Solution

For each group of controls and its associated form-submit button, set the ValidationGroup attribute to a unique group name and set the CausesValidatio attribute of the form-submit buttons to True.

In the .aspx file:

  1. Set the ValidationGroup attribute of a form-submit button to a unique group name.

  2. Set the ValidationGroup attribute of each of the controls to be validated when the button is clicked to the same group name as the form-submit button.

  3. Set the form-submit button’s CausesValidation attribute to True to have validation performed when the button is clicked.

  4. Repeat steps 1–3 for each form-submit button and its associated controls.

In the code-behind class for the page, use the .NET language of your choice to add code to the event handler for each button’s click event to check the Page.IsValid property and verify that all validation was successful. (See Recipe 3.1 for details.)

Figure 3-14 shows a typical form with normal, error-free output. Figure 3-15 shows the error message that appears on the form when a validation error occurs in the login section of the form. Figure 3-16 shows the error message that appears on the form when a validation error occurs in the form’s register section. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required