CompareValidator.Operator Property
Gets or sets the comparison operation to perform.
[Visual Basic] Public Property Operator As ValidationCompareOperator [C#] public ValidationCompareOperator Operator {get; set;} [C++] public: __property ValidationCompareOperator get_Operator(); public: __property void set_Operator(ValidationCompareOperator); [JScript] public function get Operator() : ValidationCompareOperator; public function set Operator(ValidationCompareOperator);
Property Value
One of the ValidationCompareOperator values. The default value is Equal.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentException | The specified comparison operator is not one of the ValidationCompareOperator values. |
Remarks
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 the values of the input control being validated and another control, or a constant value. |
| GreaterThan | A comparison for greater than between the 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 into 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.
Note The ControlToCompare and ValueToCompare properties are ignored when this operator is used. |
Example
[Visual Basic, C#, JScript] The following example demonstrates how to use the Operator property to specify the comparison operation.
[Visual Basic] <%@ 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) Compare1.Validate() End Sub </script> </head> <body> <form runat=server> <h3>CompareValidator Example</h3> <p> Enter a value in each textbox. 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"/> </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"/> <p> <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: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-Name="verdana" Font-Size="10pt" runat="server"/> </form> </body> </html> [C#] <%@ 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; Compare1.Validate(); } </script> </head> <body> <form runat=server> <h3>CompareValidator Example</h3> <p> Enter a value in each textbox. 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"/> </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"/> <p> <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: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-Name="verdana" Font-Size="10pt" runat="server"/> </form> </body> </html> [JScript] <%@ Page Language="JScript" AutoEventWireup="True" %> <html> <head> <script runat="server"> function Button_Click(sender, e : EventArgs) { if (Page.IsValid) { lblOutput.Text = "Result: Valid!"; } else { lblOutput.Text = "Result: Not valid!"; } } function Operator_Index_Changed(sender, e : EventArgs) { Compare1.Operator = ValidationCompareOperator(ListOperator.SelectedIndex); Compare1.Validate(); } function Type_Index_Changed(sender, e : EventArgs) { Compare1.Type = ValidationDataType(ListType.SelectedIndex); Compare1.Validate(); } </script> </head> <body> <form runat=server> <h3>CompareValidator Example</h3> <p> Enter a value in each textbox. 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"/> </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"/> <p> <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: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-Name="verdana" Font-Size="10pt" 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
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
CompareValidator Class | CompareValidator Members | System.Web.UI.WebControls Namespace | ValidationCompareOperator