Using Validator Controls

Because the RequiredFieldValidator control is the first of the validator controls we'll look at in this section, it's useful to review the class hierarchy for the RequiredFieldValidator class (shown in Figure 5-3). We know that everything in the .NET Framework descends from Object, so it's no surprise that the most remote descendant of the

RequiredFieldValidator class is Object.

Object |

— WstCOnirol

Basei/j tidal1

Figure 5-3 : Object class hierarchy for RequiredFieldValidator in the .NET Framework The RequiredFieldValidator Control

To understand this hierarchy, it's useful to see the RequiredFieldValidator control at work. Login.aspx, shown in Listing 5-1, uses the RequiredFieldValidator control. Figure 5-4 shows Login.aspx and what happens when we submit the form with neither field filled in.

l|i: I... ,-|-.M -i I.:.; .,-m,.-: Hfcl

F> i« FjYTTiP* [«* iyt

0 (J ^ (3 [[PmntlB»

jjFivdiH »


^J LiVJ *

Login Page


I'JiilViC'i [


I Lft^n|


Loe«i Irftanet

Figure 5-4 : The Login.aspx page when the Login button is clicked and the fields are not filled in

The form validator controls all have an ErrorMessage attribute that in this example has been set to "*", and thus an asterisk is displayed next to any field that doesn't validate properly. The RequiredFieldValidator control is perhaps the simplest validator control—it checks to see whether a field has a value. How does a validator control work? That depends on several factors. As with many aspects of ASP.NET, it's useful to look at the HTML code that the browser sees, to determine how your request for a validator control is translated into something that a browser can work with. Listing 5-2 shows the HTML sent to the browser before the validator controls are fired (with the listing reformatted to make it easier to read).

0 0

Post a comment