All Web development efforts require form validation, but Active Server Page technology hasn't made it easy. Fortunately, ASP.NET addresses the need with validation controls.

One essential task in Web programming is the validation of values entered into forms by users. Classic ASP provides little support in this area, leaving developers to devise their own validation mechanisms. Luckily, ASP.NET has improved this situation considerably. For starters, its event-based, UI control-centric WebForms programming model greatly simplifies the task of implementing your own validation. But ASP.NET goes even further, providing direct support for validation through a set of server controls that all but eliminate the need to implement your own validations. In this article, I'll introduce and demonstrate the validation server controls shipped with ASP.NET.

Validation control hierarchy
The ASP.NET validation server controls live under the System.Web.UI.WebControls namespace, together with Web sever controls such as TextBox and Label. ASP.NET provides six concrete validation controls, shown in green in Figure A.

Figure A
ASP.NET validation controls

The ValidationSummary control doesn't actually perform validation. Instead, it provides a way to summarize the error messages from the other validation controls within the page. For the remainder of the article, when I refer to validation controls, I'm referring to those controls that actually perform validationsâ€"all of the validation controls except ValidationSummary.

Validation controls are derived directly or indirectly from the abstract BaseValidater class, which in turn derives from the Label control. At first, it seems odd that validation controls would derive from Label, but we'll see why shortly.

Validation
A validation control works by evaluating the value of an input server control on the page to see whether it meets certain criteria. The input control to evaluate is specified through the validation control's ControlToValidate attribute. You can programmatically cause a validation control to perform its evaluation by calling its Validate method, which sets the validation control's IsValid property to true if validation succeeds and false if it fails. You can cause all of the validation controls on a page to validate their input controls by calling the Page.Validate method.

The Page.IsValid property is set to true if validation succeeds for all validation controls within the page; otherwise, it's false. The Page.Validate method is automatically invoked when a button control whose CausesValidation property is set to true is clicked. CausesValidation is set to false by default for a reset or clear button. The button click event is suppressed if IsValid is false for any validation control on the page.

Do you need help with Windows? Gain advice from Builder AU forums

Comments

1

pqrs - 14/05/07

validation control fails if html tags are put into textbox content.
How 2 avoid it?

» Report offensive content

2

rter - 11/04/08

hgjhghjg

hjgjhhg

» Report offensive content

3

ramesh_no1 - 06/06/08

pls if u can forward on mail thank ful to u

how to apply state  management techniques in asp.net 2.0
with example

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

3

ramesh_no1 - 06/06/08

pls if u can forward on mail thank ful to u how to apply state management techniques in asp.net 2.0 ... more

2

rter - 04/11/08

hgjhghjg hjgjhhg ... more

1

pqrs - 14/05/07

validation control fails if html tags are put into textbox content. How 2 avoid it? ... more

Log in


Sign up | Forgot your password?

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!