</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 won’t
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 you’re 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 don’t want the func-
tionality of the login form’s Submit button to be affected by the data in the regis-
tration form; nor can we allow the login form’s 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