System.Web.UI.WebControls.BaseValidator Class

Serves as the abstract base class for validation controls.

See Also: BaseValidator Members


[System.ComponentModel.Designer("System.Web.UI.Design.WebControls.BaseValidatorDesigner, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
public abstract class BaseValidator : Label, System.Web.UI.IValidator


The System.Web.UI.WebControls.BaseValidator class provides the core implementation for all validation controls. Validation controls are used to validate user input in an associated input control. When the user enters a value that fails validation, the validation control displays an error message. Because a validation control is separated from the input control, you can position the error message anywhere on the page relative to the input control. ASP.NET provides several validation controls that perform specific types of validation. The following table describes these controls.


Validates a value against the value entered into another input control, against a constant value, or against a proper data type by setting the CompareValidator.Operator property to ValidationCompareOperator.DataTypeCheck.


Validates a value using a user-provided custom validation routine.


Validates whether a value is within a range of values.


Validates a value using a regular expression.


Validates that a value was entered in a required field.

Validation controls always validate the associated input control on the server. Validation controls also have complete client-side implementation that allows script-enabled browsers (such as Microsoft Internet Explorer version 4.0 and later) to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, avoiding the round trip of information necessary for server-side validation.

Multiple validation controls can be used with an individual input control to validate different criteria. For example, you can apply multiple validation controls on a System.Web.UI.WebControls.TextBox control. You can use a System.Web.UI.WebControls.RangeValidator control to ensure that the value entered in the System.Web.UI.WebControls.TextBox control is within a set range, and a System.Web.UI.WebControls.RequiredFieldValidator control to ensure that the user enters a value.

ASP.NET provides many controls that have the ability to post back to the server. When one of these controls has its CausesValidation property set to true, validation is performed when the control posts back to the server. The following controls have the ability to post back to the server:


Some of these controls post back to the server only when the AutoPostBack property is set to true.

These controls each have a ValidationGroup property that, when set, validates only the validation controls within the specified group when the control triggers a post back to the server. To group validation controls, set the BaseValidator.ValidationGroup property of each validation control to the same value.

To associate an input control with a validation control, use the BaseValidator.ControlToValidate property. To specify the text to display in a validation control when validation fails, use the BaseValidator.Text property. You can also display a summary of all controls that fail validation in the page by using a System.Web.UI.WebControls.ValidationSummary control. To specify the text to display in a System.Web.UI.WebControls.ValidationSummary control, use the BaseValidator.ErrorMessage property.


If you set the BaseValidator.ErrorMessage property without setting the BaseValidator.Text property, the value of the BaseValidator.ErrorMessage property is also displayed in the validation control.

When using validator controls, you should always check the results of server-side validation before performing any processing. After a postback but before your event methods are called, the page calls the validator controls and aggregates their results into the System.Web.UI.Page.IsValid property. (You can also call the validator controls explicitly using the Validate method.) In your own code, you should check that the System.Web.UI.Page.IsValid property returns true before processing input. Even though script-enabled browsers might prevent a postback from occurring on the client if a validation check has failed, you should always also check System.Web.UI.Page.IsValid in server code before processing validated data.

You can also manually perform validation. To validate all validation controls on the page, use the System.Web.UI.Page.Validate method. Individual validation controls can be validated by using the BaseValidator.Validate method of the control.


If you use the System.Web.UI.Page.IsValid property in a Page_Load method, you must first explicitly call the System.Web.UI.Page.Validate method. Because validation occurs after the System.Web.UI.Control.Load event for the page, but before the event handler for the Click or Command events, the System.Web.UI.Page.IsValid property is not updated until the System.Web.UI.Page.Validate method is called. As an alternative, you can place your code in the event handler for the Click or Command event instead of the Page_Load method.

Not all Web server controls support validation controls. The standard controls that can be validated with a validation control are:


For an input control to be validated, the System.Web.UI.ValidationPropertyAttribute attribute must be applied to the control.


When you use validator controls that derive from System.Web.UI.WebControls.BaseValidator inside an System.Web.UI.UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the System.Web.UI.UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

When validation fails, you can set the focus on the associated input control by setting the BaseValidator.SetFocusOnError property to true.

For a list of initial property values for an instance of System.Web.UI.WebControls.BaseValidator, see the BaseValidator.#ctor constructor.


For information about how to configure this control so that it generates markup that conforms to accessibility standards, see Accessibility in Visual Studio 2010 and ASP.NET 4 and ASP.NET Controls and Accessibility.


Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0,