CompareValidator 控件(常规参考)

更新:2007 年 11 月

将输入控件的值同常数值或其他输入控件的值相比较,以确定这两个值是否与由比较运算符(小于、等于、大于等等)指定的关系相匹配。

<asp:CompareValidator
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToCompare="string"
    ControlToValidate="string"
    CssClass="string"
    CultureInvariantValues="True|False"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    Operator="Equal|NotEqual|GreaterThan|GreaterThanEqual|LessThan|
        LessThanEqual|DataTypeCheck"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    Type="String|Integer|Double|Date|Currency"
    ValidationGroup="string"
    ValueToCompare="string"
    Visible="True|False"
    Width="size"
/>

备注

CompareValidator 控件使您能够将用户输入到一个输入控件(如 TextBox 控件)中的值与输入到另一个输入控件中的值或某个常数值进行比较。还可以使用 CompareValidator 控件确定输入到输入控件中的值是否可以转换为 Type 属性指定的数据类型。

通过设置 ControlToValidate 属性来指定要验证的输入控件。如果要将特定的输入控件与另一个输入控件进行比较,请用要比较的控件的名称设置 ControlToCompare 属性。

可以将一个输入控件的值同某个常数值相比较,而不是比较两个输入控件的值。通过设置 ValueToCompare 属性来指定要比较的常数值。

Operator 属性允许您指定要执行的比较类型,如大于、等于等。如果将 Operator 属性设置为 ValidationCompareOperator.DataTypeCheck,则 CompareValidator 控件将忽略 ControlToCompareValueToCompare 属性并且只表明输入控件中输入的值是否可以转换为 Type 属性指定的数据类型。

说明:

如果输入控件为空,则不调用任何验证函数且验证成功。使用 RequiredFieldValidator 控件防止用户跳过某个输入控件。

有关 CompareValidator 控件的更多信息,请参见 System.Web.UI.WebControls.CompareValidator 类。

示例

下面的代码示例演示如何使用 CompareValidator 控件来验证一个文本框中输入的值是否与另一个文本框中输入的值匹配。验证结果随后显示在页上。

<%@ Page Language="VB" AutoEventWireup="True" %>
 
<html>
<head>
   <script runat="server">
 
      Sub Button_Click(sender As Object, e As EventArgs) 
 
         If Page.IsValid Then 
         
            lblOutput.Text = "Result: Valid!"
         
         Else 
         
            lblOutput.Text = "Result: Not valid!"
         
         End If

      End Sub
 
      Sub Operator_Index_Changed(sender As Object, e As EventArgs) 

         Compare1.Operator = CType(ListOperator.SelectedIndex, ValidationCompareOperator)
         Compare1.Validate()

      End Sub

      Sub Type_Index_Changed(sender As Object, e As EventArgs) 

         Compare1.Type = CType(ListType.SelectedIndex, ValidationDataType)
         Compare2.Type = CType(ListType.SelectedIndex, ValidationDataType)
         Compare1.Validate()

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

      <h3>CompareValidator Example</h3>
      <p>
      Enter a value in each text box. Select a comparison operator<br>
      and data type. Click "Validate" to compare values.
 
      <table bgcolor="#eeeeee" cellpadding="10">

         <tr valign="top">

            <td>

               <h5>String 1:</h5>
               <asp:TextBox id="TextBox1" 
                    runat="server"/>

               <br>

               <asp:CompareValidator id="Compare1" 
                    ControlToValidate="TextBox1" 
                    ControlToCompare="TextBox2" 
                    Type="String"
                    EnableClientScript="false" 
                    Text="Failed Validation" 
                    runat="server"/>

            </td>

            <td>

               <h5>Comparison Operator:</h5>
 
               <asp:ListBox id="ListOperator" 
                    OnSelectedIndexChanged="Operator_Index_Changed" 
                    runat="server">

                  <asp:ListItem Selected Value="Equal" >Equal</asp:ListItem>
                  <asp:ListItem Value="NotEqual" >NotEqual</asp:ListItem>
                  <asp:ListItem Value="GreaterThan" >GreaterThan</asp:ListItem>
                  <asp:ListItem Value="GreaterThanEqual" >GreaterThanEqual</asp:ListItem>
                  <asp:ListItem Value="LessThan" >LessThan</asp:ListItem>
                  <asp:ListItem Value="LessThanEqual" >LessThanEqual</asp:ListItem>
                  <asp:ListItem Value="DataTypeCheck" >DataTypeCheck</asp:ListItem>

               </asp:ListBox>

            </td>

            <td>

               <h5>String 2:</h5>
               <asp:TextBox id="TextBox2" 
                    runat="server"/>
               <br>
               <asp:CompareValidator id="Compare2" 
                    ControlToValidate="TextBox2"  
                    Operator="DataTypeCheck"
                    EnableClientScript="false"
                    Text="Invalid Data Type" 
                    runat="server"/>
               <br>
               <asp:Button id="Button1"
                    Text="Validate"  
                    OnClick="Button_Click" 
                    runat="server"/>

            </td>
         </tr>

         <tr>
            <td colspan="3" align="center">

               <h5>Data Type:</h5>

               <asp:ListBox id="ListType" 
                    OnSelectedIndexChanged="Type_Index_Changed" 
                    runat="server">

                  <asp:ListItem Selected Value="String" >String</asp:ListItem>
                  <asp:ListItem Value="Integer" >Integer</asp:ListItem>
                  <asp:ListItem Value="Double" >Double</asp:ListItem>
                  <asp:ListItem Value="Date" >Date</asp:ListItem>

               </asp:ListBox>
            </td>
         </tr>
      </table>
 
      <br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
 
<html>
<head>
   <script runat="server">
 
      void Button_Click(Object sender, EventArgs e) 
      {
 
         if (Page.IsValid) 
         {
            lblOutput.Text = "Result: Valid!";
         }
         else 
         {
            lblOutput.Text = "Result: Not valid!";
         }

      }
 
      void Operator_Index_Changed(Object sender, EventArgs e) 
      {

         Compare1.Operator = (ValidationCompareOperator)ListOperator.SelectedIndex;
         Compare1.Validate();

      }

      void Type_Index_Changed(Object sender, EventArgs e) 
      {

         Compare1.Type = (ValidationDataType)ListType.SelectedIndex;
         Compare2.Type = (ValidationDataType)ListType.SelectedIndex;
         Compare1.Validate();

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

      <h3>CompareValidator Example</h3>
      <p>
      Enter a value in each text box. Select a comparison operator<br>
      and data type. Click "Validate" to compare values.
 
      <table bgcolor="#eeeeee" cellpadding="10">

         <tr valign="top">

            <td>

               <h5>String 1:</h5>
               <asp:TextBox id="TextBox1" 
                    runat="server"/>

               <br>

               <asp:CompareValidator id="Compare1" 
                    ControlToValidate="TextBox1" 
                    ControlToCompare="TextBox2" 
                    Type="String"
                    EnableClientScript="false" 
                    Text="Failed Validation" 
                    runat="server"/>

            </td>

            <td>

               <h5>Comparison Operator:</h5>
 
               <asp:ListBox id="ListOperator" 
                    OnSelectedIndexChanged="Operator_Index_Changed" 
                    runat="server">

                  <asp:ListItem Selected Value="Equal" >Equal</asp:ListItem>
                  <asp:ListItem Value="NotEqual" >NotEqual</asp:ListItem>
                  <asp:ListItem Value="GreaterThan" >GreaterThan</asp:ListItem>
                  <asp:ListItem Value="GreaterThanEqual" >GreaterThanEqual</asp:ListItem>
                  <asp:ListItem Value="LessThan" >LessThan</asp:ListItem>
                  <asp:ListItem Value="LessThanEqual" >LessThanEqual</asp:ListItem>
                  <asp:ListItem Value="DataTypeCheck" >DataTypeCheck</asp:ListItem>

               </asp:ListBox>

            </td>

            <td>

               <h5>String 2:</h5>
               <asp:TextBox id="TextBox2" 
                    runat="server"/>
               <br>
               <asp:CompareValidator id="Compare2" 
                    ControlToValidate="TextBox2"  
                    Operator="DataTypeCheck"
                    EnableClientScript="false"
                    Text="Invalid Data Type" 
                    runat="server"/>
               <br>
               <asp:Button id="Button1"
                    Text="Validate"  
                    OnClick="Button_Click" 
                    runat="server"/>

            </td>
         </tr>

         <tr>
            <td colspan="3" align="center">

               <h5>Data Type:</h5>

               <asp:ListBox id="ListType" 
                    OnSelectedIndexChanged="Type_Index_Changed" 
                    runat="server">

                  <asp:ListItem Selected Value="String" >String</asp:ListItem>
                  <asp:ListItem Value="Integer" >Integer</asp:ListItem>
                  <asp:ListItem Value="Double" >Double</asp:ListItem>
                  <asp:ListItem Value="Date" >Date</asp:ListItem>

               </asp:ListBox>
            </td>
         </tr>
      </table>
 
      <br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>

请参见

参考

CompareValidator

其他资源

验证服务器控件语法