Export (0) Print
Expand All

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 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:

Community Additions

Show:
© 2015 Microsoft