Export (0) Print
Expand All

CompareValidator.Operator Property

Gets or sets the comparison operation to perform.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[ThemeableAttribute(false)]
public ValidationCompareOperator Operator { get; set; }
<asp:CompareValidator Operator="ValidationCompareOperator" />

Property Value

Type: System.Web.UI.WebControls.ValidationCompareOperator
One of the ValidationCompareOperator values. The default value is Equal.

ExceptionCondition
ArgumentOutOfRangeException

The specified comparison operator is not one of the ValidationCompareOperator values.

Use the Operator property to specify the comparison operation to perform. The following table lists the comparison operations that are possible.

Operation

Description

Equal

A comparison for equality between the values of the input control being validated and another control, or a constant value.

NotEqual

A comparison for inequality between the values of the input control being validated and another control, or a constant value.

GreaterThan

A comparison for greater than between the values of the input control being validated and another control, or a constant value.

GreaterThanEqual

A comparison for greater than or equal to between the values of the input control being validated and another control, or a constant value.

LessThan

A comparison for less than between the values of the input control being validated and another control, or a constant value.

LessThanEqual

A comparison for less than or equal to between the values of the input control being validated and another control, or a constant value.

DataTypeCheck

A data type comparison of the value entered in the input control being validated and the data type specified by the BaseCompareValidator.Type property. Validation fails if the value cannot be converted to the specified data type.

NoteNote

The ControlToCompare and ValueToCompare properties are ignored when this operator is used.

This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

The following code example demonstrates how to use the Operator property to specify the comparison operation.

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#" 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>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft