Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

BaseValidator Class

Serves as the abstract base class for validation controls.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class BaseValidator : Label, 
	IValidator
<asp:BaseValidator />

The BaseValidator class provides the core implementation for all validation controls. Validation controls are used to validate user input in an associated input control. When the user enters a value that fails validation, the validation control displays an error message. Because a validation control is separated from the input control, you can position the error message anywhere on the page relative to the input control. ASP.NET provides several validation controls that perform specific types of validation. The following table describes these controls.

Validation control

Description

CompareValidator

Validates a value against the value entered into another input control, against a constant value, or against a proper data type by setting the CompareValidator.Operator property to ValidationCompareOperator.DataTypeCheck.

CustomValidator

Validates a value using a user-provided custom validation routine.

RangeValidator

Validates whether a value is within a range of values.

RegularExpressionValidator

Validates a value using a regular expression.

RequiredFieldValidator

Validates that a value was entered in a required field.

Validation controls always validate the associated input control on the server. Validation controls also have complete client-side implementation that allows script-enabled browsers (such as Microsoft Internet Explorer version 4.0 and later) to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, avoiding the round trip of information necessary for server-side validation.

Multiple validation controls can be used with an individual input control to validate different criteria. For example, you can apply multiple validation controls on a TextBox control. You can use a RangeValidator control to ensure that the value entered in the TextBox control is within a set range, and a RequiredFieldValidator control to ensure that the user enters a value.

ASP.NET provides many controls that have the ability to post back to the server. When one of these controls has its CausesValidation property set to true, validation is performed when the control posts back to the server. The following controls have the ability to post back to the server:

NoteNote:

Some of these controls post back to the server only when the AutoPostBack property is set to true.

These controls each have a ValidationGroup property that, when set, validates only the validation controls within the specified group when the control triggers a post back to the server. To group validation controls, set the ValidationGroup property of each validation control to the same value.

To associate an input control with a validation control, use the ControlToValidate property. To specify the text to display in a validation control when validation fails, use the Text property. You can also display a summary of all controls that fail validation in the page by using a ValidationSummary control. To specify the text to display in a ValidationSummary control, use the ErrorMessage property.

NoteNote:

If you set the ErrorMessage property without setting the Text property, the value of the ErrorMessage property is also displayed in the validation control.

When using validator controls, you should always check the results of server-side validation before performing any processing. After a postback but before your event methods are called, the page calls the validator controls and aggregates their results into the Page.IsValid property. (You can also call the validator controls explicitly using the Validate method.) In your own code, you should check that the Page.IsValid property returns true before processing input. Even though script-enabled browsers might prevent a postback from occurring on the client if a validation check has failed, you should always also check Page.IsValid in server code before processing validated data.

You can also manually perform validation. To validate all validation controls on the page, use the Page.Validate method. Individual validation controls can be validated by using the Validate method of the control.

NoteNote:

If you use the Page.IsValid property in a Page_Load method, you must first explicitly call the Page.Validate method. Because validation occurs after the Control.Load event for the page, but before the event handler for the Click or Command events, the Page.IsValid property is not updated until the Page.Validate method is called. As an alternative, you can place your code in the event handler for the Click or Command event instead of the Page_Load method.

Not all Web server controls support validation controls. The standard controls that can be validated with a validation control are:

NoteNote:

For an input control to be validated, the System.Web.UI.ValidationPropertyAttribute attribute must be applied to the control.

NoteNote:

When you use validator controls that derive from BaseValidator inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

When validation fails, you can set the focus on the associated input control by setting the SetFocusOnError property to true.

For a list of initial property values for an instance of BaseValidator, see the BaseValidator constructor.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

Notes to Inheritors:

When you inherit from the BaseValidator class, you must override the following member: EvaluateIsValid.

The following code example demonstrates how to use multiple validation controls to verify that the user enters a valid value in a TextBox control.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Button_Click(Object sender, EventArgs e) 
  {
    if (Page.IsValid)
    {
      MessageLabel.Text = "Page submitted successfully.";
    }
    else
    {
      MessageLabel.Text = "There is an error on the page.";
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Validator Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Validator Example</h3>

      Enter a number from 1 to 10.
      <asp:textbox id="NumberTextBox" 
        runat="server"/>

      <asp:rangevalidator id="NumberCompareValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"  
        type="Integer"
        display="Dynamic" 
        errormessage="Please enter a value from 1 to 10."
        maximumvalue="10"
        minimumvalue="1"  
        text="*"
        runat="server"/>

      <asp:requiredfieldvalidator id="TextBoxRequiredValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"
        display="Dynamic" 
        errormessage="Please enter a value."
        text="*"
        runat="server"/>

      <br /><br />

      <asp:button id="SubmitButton"
        text="Submit"
        onclick="Button_Click"
        runat="server"/>

      <br /><br />

      <asp:label id="MessageLabel" 
        runat="server"/>

      <br /><br />

      <asp:validationsummary
        id="ErrorSummary"
        runat="server"/>

    </form>
  </body>
</html>
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.