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 ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.