</body>
</html>
When this form is submitted, the CustomValidator control raises the ServerVal-
idate event, and the CheckUniqueUserName method is called as a result. At the
moment, our list of usernames is limited to zak and cristian. If the new user-
name matches either of these, e.IsValid is set to False, and the error message
is displayed; otherwise, we assume that the username is valid. When our
submitButton_Click event handler checks the Page.IsValid property, e.IsValid
returns False if the user entered zak or cristian, and True if the new username
is anything else.
Although this example shows a very simple CustomValidator, you can certainly
imagine the possibilities this class makes available. For example, while we wont
explore it in this book, you could create a client-side validation function for your
CustomValidator controls by means of the ClientValidationFunction property.
For details, refer to the .NET Framework SDK Documentation for the
CustomValidator control.
Validation Groups
A very useful new feature in ASP.NET 2.0, validation groups allow us to validate
individual parts of a web page independently of its other sections. This capability
proves particularly handy when youre working with complex pages that contain
many functional components. For example, consider the scenario of a single page
that contains a login form and a quick registration form, each with its own Submit
button and its own set of validation controls. Certainly we dont want the func-
tionality of the login forms Submit button to be affected by the data in the regis-
tration form; nor can we allow the login forms data to affect submission of the
registration form.
The solution to this problem is to set the controls in each of the boxes within
different validation groups. You can assign a control to a validation group using
its ValidationGroup property, as shown in the following code:
File: ValidationGroups.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Validation Groups Demo</title>
</head>
242
Chapter 6: Using the Validation Controls
<body>
<form id="Form1" runat="server">
<!-- Login Controls -->
<h1>Login</h1>
<!-- Username -->
<p>
Username:<br />
<asp:TextBox id="usernameTextBox" runat="server" />
<asp:RequiredFieldValidator id="usernameReq"
runat="server" ControlToValidate="usernameTextBox"
ErrorMessage="Username is required!"
SetFocusOnError="True" ValidationGroup="Login" />
</p>
<!-- Password -->
<p>
Password:<br />
<asp:TextBox id="passwordTextBox"
runat="server" TextMode="Password" />
<asp:RequiredFieldValidator id="passwordReq"
runat="server" ControlToValidate="passwordTextBox"
ErrorMessage="Password is required!"
SetFocusOnError="True" ValidationGroup="Login" />
</p>
<p>
<asp:Button ID="loginButton" runat="server" Text="Log In"
ValidationGroup="Login" />
</p>
<!-- Login Controls -->
<h1>Register</h1>
<!-- Username -->
<p>
Username:<br />
<asp:TextBox id="newUserNameTextBox" runat="server" />
<asp:RequiredFieldValidator id="newUserNameReq"
runat="server" ControlToValidate="newUserNameTextBox"
ErrorMessage="Username is required!"
SetFocusOnError="True" ValidationGroup="Register" />
</p>
<!-- Password -->
<p>
Password:<br />
<asp:TextBox id="newPasswordTextBox" runat="server"
TextMode="Password" />
<asp:RequiredFieldValidator id="newPasswordReq"
runat="server" ControlToValidate="newPasswordTextBox"
ErrorMessage="Password is required!"
243
Validation Groups
SetFocusOnError="True" ValidationGroup="Register" />
</p>
<p>
<asp:Button ID="registerButton" runat="server"
Text="Register" ValidationGroup="Register" />
</p>
</form>
</body>
</html>
Executing this page reveals the two sets of controls: one for logging in an existing
user, and another for registering a new user. To keep things simple, the only
validation weve implemented in this example is achieved through
RequiredFieldValidator controls.
Clicking the Log In button triggers only those validators that share that buttons
ValidationGroup setting, as Figure 6.10 indicates.
Figure 6.10. Triggering the Login ValidationGroup
Likewise, clicking the Register button triggers the second set of validators, and
deactivates the first, as Figure 6.11 shows.
244
Chapter 6: Using the Validation Controls

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second 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.