BaseCompareValidator.CanConvert Method
Determines whether the specified string can be converted to the specified data type.
[Visual Basic] Public Shared Function CanConvert( _ ByVal text As String, _ ByVal type As ValidationDataType _ ) As Boolean [C#] public static bool CanConvert( string text, ValidationDataType type ); [C++] public: static bool CanConvert( String* text, ValidationDataType type ); [JScript] public static function CanConvert( text : String, type : ValidationDataType ) : Boolean;
Parameters
- text
- The string to test.
- type
- One of the ValidationDataType enumeration values.
Return Value
true if the specified data string can be converted to the specified data type; otherwise, false.
Remarks
Use the CanConvert method to determine whether the specified string can be converted to the specified data type. This method is commonly used to test whether a string can be converted to a compatible data type before performing an operation that depends on that data type.
Note Since this method is static (Shared in Visual Basic), you can use it without creating an instance of the class by qualifying the method name along with the class name. For example, BaseCompareValidator.CanConvert.
Example
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Button_Click(sender As Object, e As EventArgs) ' Display whether the value of TextBox1 passes validation. If Page.IsValid Then lblOutput.Text = "Validation passed! " ' An input control passes validation if the value it is being ' compared to cannot be converted to the data type specified ' by the BaseCompareValidator.Type property. Be sure to use ' validation controls on each input control independently. ' Test the values being compared for their data types. ValidateType(Page.IsValid) Else lblOutput.Text = "Validation failed! " ' Test the values being compared for their data types. ValidateType(Page.IsValid) End If End Sub Sub ValidateType(Valid As Boolean) ' Display an error message if the value of TextBox1 cannot be ' converted to the data type specified by the ' BaseCompareValidator.Type property (in this case an integer). If Not BaseCompareValidator.CanConvert(TextBox1.Text, ValidationDataType.Integer) Then lblOutput.Text &= "The first value is not an integer. " End If ' Display an error message if the value of TextBox2 cannot be ' converted to the data type specified by the ' BaseCompareValidator.Type property (in this case an integer). If Not BaseCompareValidator.CanConvert(TextBox2.Text, ValidationDataType.Integer) Then ' An input control passes validation if the value it is being ' compared to cannot be converted to the data type specified ' by the BaseCompareValidator.Type property. ' Display a different message when this scenario occurs. If Valid Then lblOutput.Text &= "However, the second value is not an integer. " Else lblOutput.Text &= "The second value is not an integer. " End If End If End Sub </script> </head> <body> <form runat=server> <h3>BaseCompareValidator CanConvert Example</h3> <p> Enter an integer in each text box. <br> Click "Validate" to compare the values <br> for equality. <table bgcolor="#eeeeee" cellpadding=10> <tr valign="top"> <td> <h5>Value 1:</h5> <asp:TextBox id="TextBox1" runat="server"/> </td> <td> <h5>Value 2:</h5> <asp:TextBox id="TextBox2" runat="server"/> <p> <asp:Button id="Button1" Text="Validate" OnClick="Button_Click" runat="server"/> </td> </tr> </table> <asp:CompareValidator id="Compare1" ControlToValidate="TextBox1" ControlToCompare="TextBox2" EnableClientScript="False" Type="Integer" 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) { // Display whether the value of TextBox1 passes validation. if (Page.IsValid) { lblOutput.Text = "Validation passed! "; // An input control passes validation if the value it is being // compared to cannot be converted to the data type specified // by the BaseCompareValidator.Type property. Be sure to use // validation controls on each input control independently. // Test the values being compared for their data types. ValidateType(Page.IsValid); } else { lblOutput.Text = "Validation failed! "; // Test the values being compared for their data types. ValidateType(Page.IsValid); } } void ValidateType(bool Valid) { // Display an error message if the value of TextBox1 cannot be // converted to the data type specified by the // BaseCompareValidator.Type property (in this case an integer). if (!BaseCompareValidator.CanConvert(TextBox1.Text, ValidationDataType.Integer)) { lblOutput.Text += "The first value is not an integer. "; } // Display an error message if the value of TextBox2 cannot be // converted to the data type specified by the // BaseCompareValidator.Type property (in this case an integer). if (!BaseCompareValidator.CanConvert(TextBox2.Text, ValidationDataType.Integer)) { // An input control passes validation if the value it is being // compared to cannot be converted to the data type specified // by the BaseCompareValidator.Type property. // Display a different message when this scenario occurs. if(Valid) { lblOutput.Text += "However, the second value is not an integer. "; } else { lblOutput.Text += "The second value is not an integer. "; } } } </script> </head> <body> <form runat=server> <h3>BaseCompareValidator CanConvert Example</h3> <p> Enter an integer in each text box. <br> Click "Validate" to compare the values <br> for equality. <table bgcolor="#eeeeee" cellpadding=10> <tr valign="top"> <td> <h5>Value 1:</h5> <asp:TextBox id="TextBox1" runat="server"/> </td> <td> <h5>Value 2:</h5> <asp:TextBox id="TextBox2" runat="server"/> <p> <asp:Button id="Button1" Text="Validate" OnClick="Button_Click" runat="server"/> </td> </tr> </table> <asp:CompareValidator id="Compare1" ControlToValidate="TextBox1" ControlToCompare="TextBox2" EnableClientScript="False" Type="Integer" runat="server"/> <br> <asp:Label id="lblOutput" Font-Name="verdana" Font-Size="10pt" runat="server"/> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# 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