BaseCompareValidator.CanConvert Method

Definition

Determines whether the specified string can be converted to the specified data type.

Overloads

CanConvert(String, ValidationDataType)

Determines whether the specified string can be converted to the specified data type. This version of the overloaded method tests currency, double, and date values using the format used by the current culture.

CanConvert(String, ValidationDataType, Boolean)

Determines whether the specified string can be converted to the specified data type. This version of the overloaded method allows you to specify whether values are tested using a culture-neutral format.

CanConvert(String, ValidationDataType)

Determines whether the specified string can be converted to the specified data type. This version of the overloaded method tests currency, double, and date values using the format used by the current culture.

C#
public static bool CanConvert(string text, System.Web.UI.WebControls.ValidationDataType type);

Parameters

text
String

The string to test.

type
ValidationDataType

One of the ValidationDataType values.

Returns

true if the specified data string can be converted to the specified data type; otherwise, false.

Examples

The following example demonstrates using the CanConvert method to compare two integer values and determine whether the second integer can be converted.

ASP.NET (C#)

<%@ 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 runat="server">
    <title>BaseCompareValidator CanConvert Example</title>
<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 id="form1" 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.
      </p>
 
      <table style="background-color:#eeeeee; padding: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"/>
                </p>
            </td>
         </tr>

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

Remarks

Use the CanConvert(String, ValidationDataType) 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.

This version of the method tests the value using the format used by the current culture. To test the value using a culture-neutral format, use the BaseCompareValidator.CanConvert(String, ValidationDataType, Boolean) overloaded version of this method.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

CanConvert(String, ValidationDataType, Boolean)

Determines whether the specified string can be converted to the specified data type. This version of the overloaded method allows you to specify whether values are tested using a culture-neutral format.

C#
public static bool CanConvert(string text, System.Web.UI.WebControls.ValidationDataType type, bool cultureInvariant);

Parameters

text
String

The string to test.

type
ValidationDataType

One of the ValidationDataType enumeration values.

cultureInvariant
Boolean

true to test values using a culture-neutral format; otherwise, false.

Returns

true if the specified data string can be converted to the specified data type; otherwise, false.

Remarks

Use the CanConvert(String, ValidationDataType, Boolean) 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. To indicate that values should be tested using a culture-neutral format, pass in true for the cultureInvariant parameter; otherwise, values are tested using the format used by the current culture. When testing a value using the format used by the current culture, consider using the BaseCompareValidator.CanConvert(String, ValidationDataType) overloaded version of this method.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1