共用方式為


Introduction to the CustomValidator Control 

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

ASP .NET Custom Validator

Like other validation controls, the CustomValidator control validates input from the TextBox or SelectionList control. Set the CustomValidator control's ControlToValidate property to the ID of the control to validate. When a form that contains a CustomValidator control is posted to the server, the CustomValidator control raises its ServerValidate event. You must provide a handler with the following signature:

Private Sub EventHandlerName(
    ByVal source As System.Object, 
    ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    Handles CustomValidator1.ServerValidate
private void EventHandlerName(
    object source, 
    System.Web.UI.WebControls.ServerValidateEventArgs args) { }

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

In the Visual Basic version of the function signature, the name CustomValidator1 refers to the ID 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 is the name of a predefined style in the default StyleSheet.

See Also

Tasks

How to: Add and Configure a CustomValidator Control
How to: Add and Configure a ValidationSummary Control

Reference

CustomValidator

Concepts

Introduction to the Validation Controls