7.10 Perform Selective Input Validation


Problem

You need to use ASP.NET validation controls. However, you want to perform validation programmatically so that you can validate only specific controls or sets of controls, or so that you can customize validation error messages based on the invalid input.

Solution

Disable the EnableClientScript property of every validation control on your page so that the page can be posted back. Then use the Page.Validate method to validate the page or the BaseValidator.Validate method to validate individual controls.

Discussion

The ASP.NET validation controls are an ideal solution for quickly validating forms. They work well as long as you want to validate an entire page at a time. If you want to validate only part of a form, or you want to make a programmatic decision about whether to validate a control (perhaps based on the validation success or value of another control), you'll need to use selective validation.

The first step in selective validation is to disable the EnableClientScript property of every validation control on your page. Otherwise, validation will be performed at the client through JavaScript, the page won't be posted back if it contains invalid values, and your event handling code won't be executed. Once you've made this change, you can validate the page one control at a time using the BaseValidator.Validate method, or you can validate the entire page using the Page.Validate method.

The following example uses server-side validation with two validators: a RangeValidator and a RegularExpressionValidator (which validates an e-mail address). If validation fails, the code steps through the collection of validators on the form using the Page.Validators property. Every time the code finds a failed validator, it finds the corresponding control using the Page.FindControl method and then displays the offending value (a trick that's not possible with automatic validation).

 using System; using System.Web; using System.Web.UI.WebControls; public class SelectiveValidation : System.Web.UI.Page {     protected System.Web.UI.WebControls.TextBox txtNumber;     protected System.Web.UI.WebControls.TextBox txtEmail;     protected System.Web.UI.WebControls.Label lblCustomSummary;     protected System.Web.UI.WebControls.RegularExpressionValidator       validatorEmail;     protected System.Web.UI.WebControls.RangeValidator validatorNumber;     protected System.Web.UI.WebControls.Button cmdValidate;     // (Designer code omitted.)     private void cmdValidate_Click(object sender, System.EventArgs e) {              // Validate the page.         this.Validate();         if (!Page.IsValid) {                      lblCustomSummary.Text = "";             foreach (BaseValidator validator in this.Validators) {                              if (!validator.IsValid) {                                      TextBox invalidControl = (TextBox)                       this.FindControl(validator.ControlToValidate);                     lblCustomSummary.Text +=                       "The page contains the following error: <b>" +                       validator.ErrorMessage + "</b>.<br>" +                       "The invalid input is: <b>" +                       invalidControl.Text + "</b>." + "<br><br>";                 }             }         }else {             lblCustomSummary.Text = "Validation succeeded.";         }     } } 

The form is shown, with invalid input, in Figure 7.6.

click to expand
Figure 7.6: Using custom validation.



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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