Click to Rate and Give Feedback
MSDN
MSDN Library
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET Mobile Web Pages
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:

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) { }

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:

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker