This documentation is archived and is not being maintained.

Introduction to the CustomValidator Control

Visual Studio .NET 2003

If the validation controls provided by the ASP.NET mobile controls do not suit your needs, use the CustomValidator control, below, to create a control that performs customized validation.

Like other validation controls, the CustomValidator control validates input from the TextBox or SelectionList control. Store the ID of the control to validate in the CustomValidator control's ControlToValidate property.

When a form that contains a CustomValidator control is posted to the application server, the CustomValidator control raises its ServerValidate event. You must provide a handler for this event in your application. The event handler must match one of the following signatures.

' Visual Basic
Private Sub EventHandlerName(
    ByVal source As System.Object, 
    ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate

// C#
private void EventHandlerName(
    object source, 
    System.Web.UI.WebControls.ServerValidateEventArgs args);

The name EventHandlerName in these examples represents the name of the method that handles the ServerValidate event. The parameter source is a reference to the CustomValidator control calling this event handler. The parameter args contains the user input to validate. The event handler receives the user's input in the args.Value property. If the input is valid, it sets args.IsValid to true. If the event handler for the ServerValidate event sets args.IsValid to false, the CustomValidator control displays the text in its ErrorMessage property.

In the Visual Basic version of the function signature, the name CustomValidator1 refers to the name of the CustomValidator control.

When you add a CustomValidator control to a container control or template:

  • The ErrorMessage property is set to CustomValidator.
  • The StyleReference property is set to error, which maps to a predefined style by default.

See Also

CustomValidator Control | CustomValidator Class | Adding and Configuring a CustomValidator Control | Introduction to the Validation Controls

Show: