Validation

One issue that was identified with the ASP.NET validation controls in version 1.x was that they all belonged to the same " group " within a page. For example, take a page that contains a login and password text box for existing users, plus a set of controls where new users can register their details instead. In Internet Explorer, the page cannot be submitted because the client-side validation checks will fail on the controls that have no values. In other browsers the page can be submitted, but the Page.IsValid property will return False .

One way around this is to arrange to disable the validators on the sets of controls for which users are not entering values, but this depends on knowing which sets of controls they will use. The other alternative is to disable all the validators, then enable the appropriate ones and call their Validate method in code when the page is submitted.

The ValidationGroup Property

In ASP.NET 2.0, the problem goes away courtesy of the new Validation Group property exposed by the BaseValidator control from which all the validation controls inherit. You assign the same String value to this property for all the validators that are in the same group, and different values for different groups. This is particularly useful for those scenarios where multiple forms are required or on pages that use the Wizard and MultiView controls.

The three controls you can use to submit a form (the Button , Link Button , and ImageButton controls) also expose the ValidationGroup property, and you set this to the same value as the validators in the group that the button corresponds to. For example, in Figure 9.1, the page contains a Login and a Register section.

Figure 9.1. An ASP.NET page with two validation groups

graphics/09fig01.gif

All the validation controls in the Login section at the top carry the attribute ValidationGroup="LoginGroup" , as does the Button control labeled Log in. All the validation controls in the lower Register section carry the attribute ValidationGroup="RegisterGroup" , as does the Button control labeled Register.

When the page is submitted, an event handler defined in the OnClick attribute of the Button controls is executed. For example, the Log in button has the OnClick="DoLogin" attribute so the event handler named DoLogin runs. In it we check whether the user 's values are valid by examining the IsValid property of the current Page :

 If Page.IsValid Then   ... code to run when valid values have been provided ... Else   ... code to run when the values provided are not valid ... End If 

In ASP.NET 2.0, the IsValid property now automatically takes into account the value of the ValidationGroup property, using the value set for this property on the button that caused the postback. So, if the user clicks the Log in button, which carries the attribute ValidationGroup="LoginGroup" , only the validation controls with the same value for their ValidationGroup property are checked. Validators and buttons with no ValidationGroup property set are regarded as being in the default group, so ASP.NET 1.x pages will continue to work as before under ASP.NET 2.0.

Table 9.1. Page Validation Methods

Method

Description

Page.GetValidators(" group_name ")

Returns a collection of validation controls that have the specified value for their ValidationGroup property.

Page.Validate(" group_name ")

Performs validation for only the validation controls that have the specified value for their ValidationGroup property. Returns True if all these contain valid values, or returns False otherwise .

To go with the new ValidationGroup property are a couple of new or changed methods for the Page class (see Table 9.1). These allow you to interact with the validation controls in code at runtime.

The SetFocusOnError Property

Another new feature of the validation controls is the ability to make the page more user-friendly by automatically setting the input focus to the first control that contains an invalid value. All the validation controls now expose the SetFocusOnError property, which you can set to True (in code or by adding an attribute when you declare the control) to turn on this feature. The default if not specified is False .



A First Look at ASP. NET v. 2.0 2003
A First Look at ASP. NET v. 2.0 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 90

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