High-Performance Development - C#, VB, SQL, ASP.NET

Slogan Text

Know When Validation Is Triggered

ASP.NET performs validation when:

  • the user leaves an input field.
  • the user clicks on a submit button.
  • the code calls Page.IsValid.
User Leaves Input Field

The user tabs or sets the cursor to the input field, such as, a TextBox enters characters and then tabs to the next field. When the cursor leaves the input field ASP.NET validates the input field contents or selection. The ErrorMessage is displayed in the selected destination.

This validation action occurs when you use the built-in Validator controls, such as, RegularExpression validator.

The value is that the user is still focused on the input field when they read the validation error (or not if the input is valid.).

User Clicks on Submit Button

The user fills in some fields or makes selections and then clicks on the Submit button. This causes the ASP.NET framework to execute all the validation controls for the page or validation group assigned to the Submit button.

You must use this validation approach when the user must fill in or select two or more items in a drop down list. For example, you might have a start and stop dates text boxes. The validation rule is the start date must be prior to the stop date. You can't check this validation as the user is entering the dates. Check the start against the stop date when the user clicks on the Submit button.

Code Calls Page.IsValid

The user fills in some fields or makes selections in drop down lists and clicks on the Submit button. The ASP.NET framework does not walk through the list of validation controls for the page or the validation group (I believe the validation methods are not called when using a DataGrid). In the code-behind ItemCommand event call Page.IsValid. If true is proceed with processing the form data per your application requirements. Otherwise, return from the Update code. ASP.NET displays the validation error messages.

If you're using a CustomValidator, which does server-side validation, you'll need to call PAge.IsValid in order to invoke the ServerValidate event.