Export (0) Print
Expand All

BaseValidator Class

Serves as the abstract base class for validation controls.

For a list of all members of this type, see BaseValidator Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.Label
            System.Web.UI.WebControls.BaseValidator
               System.Web.UI.WebControls.BaseCompareValidator
               System.Web.UI.WebControls.CustomValidator
               System.Web.UI.WebControls.RegularExpressionValidator
               System.Web.UI.WebControls.RequiredFieldValidator

[Visual Basic]
MustInherit Public Class BaseValidator
   Inherits Label
   Implements IValidator
[C#]
public abstract class BaseValidator : Label, IValidator
[C++]
public __gc __abstract class BaseValidator : public Label,
   IValidator
[JScript]
public abstract class BaseValidator extends Label implements
   IValidator

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The BaseValidator class provides the core implementation for all validation controls.

Validation is performed when a button control, such a Button or LinkButton, is clicked. The following table lists the button controls that invoke validation.

Server Control Description
Button Creates a push button Web server control.
HtmlButton Creates a button HTML server control that allows you to programmatically access the <button> HTML element.
HtmlInputButton Creates a button HTML server control that allows you to programmatically access the <input type=button>, <input type=submit>, or <input type=reset> HTML elements.
HtmlInputImage Creates a button HTML server control that allows you to programmatically access the <input type=image> HTML element.
ImageButton Creates a button Web server control that displays an image.
LinkButton Creates a button Web server control with the appearance of a hyperlink.

You can also manually perform validation by using the Page.Validate method.

Note   If you use the Page.IsValid property in the Page_Load method, you must first explicitly call the Page.Validate method. Since 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 Page_Load method.

Validation controls always validate the associated input control on the server. Validation controls also have complete client-side implementation that allow DHTML supported browsers (such as 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 that allow the user to enter the quantity of items to add to a shopping cart. You can use a RangeValidator control to ensure that the value specified is within a set range and a RequiredFieldValidator control to ensure that the user enters a value into the TextBox control.

If an input control fails validation, the text specified by the ErrorMessage property is displayed in the validation control. This text is also displayed in the ValidationSummary control, if one is placed on the Web page.

It is possible to display different text for the validation control and the ValidationSummary control. Use the Text property to specify the text to display in the validation control. The text specified by the ErrorMessage property is always used in the ValidationSummary control.

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

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

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use multiple validation controls to verify that the user enters a valid value in a TextBox control.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>
   <script runat="server">
 
      Sub Button_Click(sender As Object, e As EventArgs) 

         Dim rand_number As Random = New Random()

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString()
         Compare1.Validate()
 
         If Page.IsValid Then 

            lblOutput.Text = "You guessed correctly!!"
         
         Else 
         
            lblOutput.Text = "You guessed poorly"
         
         End If

         lblOutput.Text += "<br><br>" & "The number is: " & Compare1.ValueToCompare

      End Sub
 
   </script>
 
</head>
<body>
 
   <form runat=server>

      <h3>Validator Example</h3>

      <h5>Pick a number between 1 and 10:</h5>
     
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"
           EnableClientScript="False"  
           Type="Integer"
           Display="Dynamic" 
           ErrorMessage="Incorrect guess!!"
           Text="*"
           runat="server"/>

      <asp:RequiredFieldValidator id="Require1" 
           ControlToValidate="TextBox1"
           EnableClientScript="False"
           Display="Dynamic" 
           ErrorMessage="No number entered!!"
           Text="*"
           runat="server"/>

      <br><br>

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>
 
      <br><br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <br><br>

      <asp:ValidationSummary
           id="Summary1"
           runat="server"/>
 
   </form>
 
</body>
</html>


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
   <script runat="server">
 
      void Button_Click(Object sender, EventArgs e) 
      {

         Random rand_number = new Random();

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString();
         Compare1.Validate();
 
         if (Page.IsValid) 
         {
            lblOutput.Text = "You guessed correctly!!";
         }
         else 
         {
            lblOutput.Text = "You guessed poorly";
         }

         lblOutput.Text += "<br><br>" + "The number is: " + Compare1.ValueToCompare;

      }
 
   </script>
 
</head>
<body>
 
   <form runat=server>

      <h3>Validator Example</h3>

      <h5>Pick a number between 1 and 10:</h5>
     
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"
           EnableClientScript="False"  
           Type="Integer"
           Display="Dynamic" 
           ErrorMessage="Incorrect guess!!"
           Text="*"
           runat="server"/>

      <asp:RequiredFieldValidator id="Require1" 
           ControlToValidate="TextBox1"
           EnableClientScript="False"
           Display="Dynamic" 
           ErrorMessage="No number entered!!"
           Text="*"
           runat="server"/>

      <br><br>

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>
 
      <br><br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <br><br>

      <asp:ValidationSummary
           id="Summary1"
           runat="server"/>
 
   </form>
 
</body>
</html>


[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
<head>
   <script language="JScript" runat="server">
 
      function Button_Click(sender, e : EventArgs) 
      {

         var rand_number : Random  = new Random();

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString();
         Compare1.Validate();
 
         if (Page.IsValid) 
         {
            lblOutput.Text = "You guessed correctly!!";
         }
         else 
         {
            lblOutput.Text = "You guessed poorly";
         }

         lblOutput.Text += "<br><br>" + "The number is: " + Compare1.ValueToCompare;

      }
 
   </script>
 
</head>
<body>
 
   <form runat=server>

      <h3>Validator Example</h3>

      <h5>Pick a number between 1 and 10:</h5>

      <asp:RequiredFieldValidator id="Require1" 
           ControlToValidate="TextBox1"
           Type="Integer" 
           ErrorMessage="No number entered!!"
           Text="*"
           runat="server"/>
     
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"
           EnableClientScript="False"  
           Type="Integer" 
           ErrorMessage="Incorrect guess!!"
           Text="*"
           runat="server"/>

      <br><br>

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>
 
      <br><br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <br><br>

      <asp:ValidationSummary
           id="Summary1"
           runat="server"/>
 
   </form>
 
</body>
</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

BaseValidator Members | System.Web.UI.WebControls Namespace | CompareValidator | CustomValidator | RangeValidator | RegularExpressionValidator | RequiredFieldValidator | ValidationSummary

Show:
© 2014 Microsoft