Main Page

  Previous Section Next Section


System.Web.UI.WebControls (system.web.dll)class

The CustomValidator control allows you to define your own validation routines. A similar task could be performed by writing manual validation code in the click event for a submit button, but using a CustomValidator allows you to configure client-side validation, create an error message that will be included in the validation summary, and provide a "vote" used for the combined System.Web.UI.Page.IsValid property along with all other validation controls. A CustomValidator can also be reused easily to validate multiple controls.

To provide server-side validation, create an event handler for the ServerValidate event. The string from the input control and the result of the validation is stored in the provided ServerValidateEventArgs object. You can also perform client-side validation, which can improve the responsiveness of your application by reducing the need for round trips to the server. However, because client-side validation will not be performed in some browsers and is easy to circumvent, it should never be used in place of server validation. To use client-side validation, set the ClientValidationFunction to the name of a JavaScript or VBScript function in the .aspx code portion of your page (not the code-behind class). This script function should be in a language that the client browser can understand, which means that JavaScript is the best choice if you are supporting non-Microsoft browsers. The function must be in the form function ValidationFunctionName(source, value), where value is the value to be validated. This function should return True or False to indicate whether the validation succeeded. ASP.NET will take care of the code necessary to display the corresponding error message. Note that this is a very bad idea if your function uses "secret" logic to validate a password or key, as this logic will be easily retrievable by the client!

public class CustomValidator : BaseValidator {
// Public Constructors
   public CustomValidator( );
// Public Instance Properties
   public string ClientValidationFunction{set; get; }
// Protected Instance Methods
   protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer); // overrides BaseValidator
   protected override bool ControlPropertiesValid( );             // overrides BaseValidator
   protected override bool EvaluateIsValid( );       // overrides BaseValidator
   protected virtual bool OnServerValidate(string value);
// Events
   public event ServerValidateEventHandler ServerValidate;


System.Object System.Web.UI.Control(System.ComponentModel.IComponent, System.IDisposable, System.Web.UI.IParserAccessor, System.Web.UI.IDataBindingsAccessor) WebControl(System.Web.UI.IAttributeAccessor) Label BaseValidator(System.Web.UI.IValidator) CustomValidator

      Previous Section Next Section