3.0 Introduction


ASP.NET validation controls (also known as validators) simplify the task of ensuring that data is entered correctly on forms. For most validations, no code is required in the .aspx file or the code-behind class. You add a validator to the .aspx file, have it reference an input control (a server control) elsewhere on the page, and set one or more of its validation attributes (such as MinimumValue or MaximumValue, which specify the minimum and maximum values of a validation range). ASP.NET does all the rest. You can combine validators to provide multiple validations on a single input, such as a RequiredFieldValidator and a RangeValidator, which perform as their names imply.

Validation can be performed on the client and server. By default, validators perform their validation automatically on postback in server code. However, if the user has a browser that supports DHTML and client-side validation is enabled, validators can perform their validation using client script. Client-side validation is handy whenever you want to avoid a round trip to the server for server-side validation, such as when you want to make sure an entry is provided in a text box. Regardless of whether client-side validation is performed, server-side validation is always a good idea if only to ensure that validation takes place, even when the user's browser doesn't support DHTML.

This chapter includes a useful collection of recipes for validating data, starting with automatic, attribute-oriented validation, next dealing with custom validation, and ending with validation groups. When you perform custom validation, you intercept an input control's validation call and provide your own validation logic (by adding your own custom JavaScript and server-side code). Custom validation is the focus of two of the chapter's recipes, which show you how to require a user to make a selection from a drop-down list and how to require valid user input data, such as a password that matches an entry in a database.

Validation groups are new to ASP.NET 2.0 and were introduced to support the concept of multiple logical sections on a form. A common scenario is when you have two logical sections on a form, one for supporting login and the other for new user registration, both with their own Submit buttons, such as that shown in Figure 3-14. Handling this scenario in ASP.NET 1.x was difficult because clicking either button would invoke all the validators for the page, which was not generally intended. With ASP.NET 2.0, however, you can group controls for purposes of validation and associate them with the button the user clicks. That is, you can have one group of controls validated when a Login button is clicked and another when a Register button is clicked. The last two recipes show you how to handle this scenario easily using validation groups. The first shows the basics and the second goes into more depth by showing you how to handle the validation under programmatic control, which is useful when you want to perform your own nonstandard validation, such as when you want to check a new user's registration against a database.

All validators, except the RequiredFieldValidator, allow the control being validated to be left blank. This subtle point is worth noting, as you may need to account for this behavior in your code when using ASP.NET's automatic validation.




ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net