Build Your Own ASP.NET 3.5 Website Using C# & VB (46 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
11.38Mb size Format: txt, pdf, ePub

<%@ Page Language="C#" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<br/><b>Custom Validator<br/></b>






New Username:


Licensed to [email protected]

252

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

ControlToValidate="usernameTextBox"

OnServerValidate="CheckUniqueUserName"

ErrorMessage="This username already taken!" />



OnClick="submitButton_Click" Text="Submit" />






When this form is submitted, the CustomValidator control raises the ServerValidate event, and the CheckUniqueUserName method is called as a result. At the moment, our list of usernames is limited to andrei and cristian. If the new username 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 andrei 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 feature of ASP.NET, 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 functionality of the login

form’s
Submit
button to be affected by the data in the registration form; nor can we

allow the login form’s data to affect submission of the registration form.

Licensed to [email protected]

Using the Validation Controls

253

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:

LearningASP\VB\ValidationGroups.aspx
(excerpt)

<%@ Page Language="VB" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Untitled Page






Login



Username:


runat="server" ControlToValidate="usernameTextBox"

ErrorMessage="Username is required!"

SetFocusOnError="True"
ValidationGroup="Login"
/>




Password:

TextMode="Password" />

runat="server" ControlToValidate="passwordTextBox"

ErrorMessage="Password is required!"

SetFocusOnError="True"
ValidationGroup="Login"
/>



254

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

ValidationGroup="Login" />




Register



Username:


runat="server" ControlToValidate="newUserNameTextBox"

ErrorMessage="Username is required!"

SetFocusOnError="True"
ValidationGroup="Register"
/>




Password:

TextMode="Password" />

runat="server" ControlToValidate="newPasswordTextBox"

ErrorMessage="Password is required!"

SetFocusOnError="True"
ValidationGroup="Register"
/>



Text="Register"
ValidationGroup="Register"
/>






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 we’ve implemented in this example is achieved through RequiredFieldValidator controls.

Clicking the
Log In
button triggers only those validators that share that button’s

ValidationGroup
setting, as Figure 6.10 indicates.

Licensed to [email protected]

Using the Validation Controls

255

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.

Default Validation Groups

Controls that aren’t specifically assigned to any validation group are aggregated

into a default validation group. In other words, a button that isn’t assigned to any

validation group will trigger only those validation controls that aren’t assigned

to any groups.

Finally, remember that Page.IsValid returns the results of the current validation

group (that is, the one that caused the server-side event). To verify the validity of

another group on the page, we use the Page.Validate method, which can receive

as a parameter the name of the validation group to be validated.

Licensed to [email protected]

256

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Figure 6.11. Activating the RegisterValidationGroup

Updating Dorknozzle

Now that you’ve spent some time with validation controls, let’s use them to update

Dorknozzle’s Help Desk page. The following rules must be met before the user can

submit a new help desk request:

■ The station number text box cannot be empty.

■ The station number must be a valid number.

■ The station number must be a numeral between 1 and 50.

■ A description of the problem must be entered.

To make changes to the Help Desk page, you first need to load the Dorknozzle project

in Visual Web Developer. Go to
File
>
Open Web Site…
and select the Dorknozzle project.

Licensed to [email protected]

Other books

Firebird by Jack McDevitt
Marshmallows for Breakfast by Dorothy Koomson
Russian Literature by Catriona Kelly
American Fun by John Beckman
Memories by Sletten, Deanna Lynn
Frozen: Heart of Dread, Book One by de la Cruz, Melissa, Johnston, Michael
Shakespeare's Trollop by Charlaine Harris
Lucky in Love by Jill Shalvis