ValidationCompareOperator 列舉

定義

指定 CompareValidator 控制項所使用的驗證比較運算子。

public enum class ValidationCompareOperator
public enum ValidationCompareOperator
type ValidationCompareOperator = 
Public Enum ValidationCompareOperator
繼承
ValidationCompareOperator

欄位

DataTypeCheck 6

只限資料型別的比較。

Equal 0

相等的比較。

GreaterThan 2

大於的比較。

GreaterThanEqual 3

大於或等於的比較。

LessThan 4

小於的比較。

LessThanEqual 5

小於或等於的比較。

NotEqual 1

不相等的比較。

範例

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ Page Language="C#" AutoEventWireup="True" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>CompareValidator Example</title>
<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;
         Compare1.Validate();

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

      <h3>CompareValidator Example</h3>
      <br />
      Enter a value in each textbox. Select a comparison operator<br />
      and data type. Click "Validate" to compare values.
 
      <table style="background-color:#eeeeee; padding:10">

         <tr valign="top">

            <td>

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

            </td>

            <td>

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

                  <asp:ListItem Selected="True" 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: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="true" 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:ListItem Value="Currency" >Currency</asp:ListItem>

               </asp:ListBox>
            </td>
         </tr>
      </table>
 
      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1" 
           ControlToCompare="TextBox2"
           EnableClientScript="False" 
           Type="String" 
           runat="server"/>
 
      <br />
       
      <asp:Label id="lblOutput" 
           Font-Names="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>CompareValidator Example</title>
<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)
         Compare1.Validate()

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

      <h3>CompareValidator Example</h3>
      <br />
      Enter a value in each textbox. Select a comparison operator<br />
      and data type. Click "Validate" to compare values.
 
      <table style="background-color:#eeeeee; padding:10">

         <tr valign="top">

            <td>

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

            </td>

            <td>

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

                  <asp:ListItem Selected="True" 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: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="true" 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:ListItem Value="Currency" >Currency</asp:ListItem>

               </asp:ListBox>
            </td>
         </tr>
      </table>
 
      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1" 
           ControlToCompare="TextBox2"
           EnableClientScript="False" 
           Type="String" 
           runat="server"/>
 
      <br />
       
      <asp:Label id="lblOutput" 
           Font-Names="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>

備註

列舉 ValidationCompareOperator 表示控制項可執行檔 CompareValidator 比較作業。

注意

與其他運算子不同, DataTypeCheck 是一元運算。 只會檢查正在驗證之輸入控制項的資料類型。 ControlToCompare如果使用此作業時會設定 或 ValueToCompare 屬性,則不會檢查這些值。

適用於

另請參閱