BaseCompareValidator.Type Property
Gets or sets the data type that the values being compared are converted to before the comparison is made.
[Visual Basic] Public Property Type As ValidationDataType [C#] public ValidationDataType Type {get; set;} [C++] public: __property ValidationDataType get_Type(); public: __property void set_Type(ValidationDataType); [JScript] public function get Type() : ValidationDataType; public function set Type(ValidationDataType);
Property Value
One of the ValidationDataType enumeration values. The default value is String.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentException | The specified data type is not one of the ValidationDataType values. |
Remarks
Use the Type property to specify the data type used for comparison. The Type property is used differently by the various comparison validation controls. For example, in the RangeValidator control, all values being compared (the upper bound, lower bound, and value of the input control) are converted to the specified data type before any comparison is performed. However, if you use a CompareValidator control and set its Operator property to ValidationCompareOperator.DataTypeCheck, only the value of the input control is converted to the specified data type.
Note If the value of the input control associated with the validation control cannot be converted to the specified data type, validation fails. The IsValid property of the validation control is set to false.
The following table lists the values that you can use for the Type property.
| Data Type | Description |
|---|---|
| String | Specifies a string data type. |
| Integer | Specifies a 32-bit signed integer data type. |
| Double | Specifies a double-precision floating-point number data type. |
| Date | Specifies a date data type. |
| Currency | Specifies a monetary data type. |
Example
[Visual Basic, C#, JScript] The following example demonstrates how to use the Type property to specify the data type that values being compared are converted to before the comparison is made.
[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
BaseCompareValidator Class | BaseCompareValidator Members | System.Web.UI.WebControls Namespace | ValidationDataType