BaseCompareValidator.CanConvert メソッド

定義

指定した文字列を指定したデータ型に変換できるかどうかを判断します。

オーバーロード

名前 説明
CanConvert(String, ValidationDataType)

指定した文字列を指定したデータ型に変換できるかどうかを判断します。 オーバーロードされたメソッドのこのバージョンでは、現在のカルチャで使用される形式を使用して、通貨、double、および日付の値をテストします。

CanConvert(String, ValidationDataType, Boolean)

指定した文字列を指定したデータ型に変換できるかどうかを判断します。 このバージョンのオーバーロードされたメソッドでは、カルチャに依存しない形式を使用して値をテストするかどうかを指定できます。

CanConvert(String, ValidationDataType)

指定した文字列を指定したデータ型に変換できるかどうかを判断します。 オーバーロードされたメソッドのこのバージョンでは、現在のカルチャで使用される形式を使用して、通貨、double、および日付の値をテストします。

public:
 static bool CanConvert(System::String ^ text, System::Web::UI::WebControls::ValidationDataType type);
public static bool CanConvert(string text, System.Web.UI.WebControls.ValidationDataType type);
static member CanConvert : string * System.Web.UI.WebControls.ValidationDataType -> bool
Public Shared Function CanConvert (text As String, type As ValidationDataType) As Boolean

パラメーター

text
String

テストする文字列。

返品

true 指定したデータ文字列を指定したデータ型に変換できる場合は 、次の値を指定します。それ以外の場合は false

次の例では、 CanConvert メソッドを使用して 2 つの整数値を比較し、2 番目の整数を変換できるかどうかを判断する方法を示します。


<%@ 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>

<%@ Page Language="VB" 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">
 
      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 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>

注釈

指定した文字列を指定したデータ型に変換できるかどうかを判断するには、 CanConvert(String, ValidationDataType) メソッドを使用します。 このメソッドは、そのデータ型に依存する操作を実行する前に、文字列を互換性のあるデータ型に変換できるかどうかをテストするために一般的に使用されます。

このバージョンのメソッドは、現在のカルチャで使用されている形式を使用して値をテストします。 カルチャに依存しない形式を使用して値をテストするには、このメソッドのオーバーロードされたバージョン BaseCompareValidator.CanConvert(String, ValidationDataType, Boolean) 使用します。

こちらもご覧ください

適用対象

CanConvert(String, ValidationDataType, Boolean)

指定した文字列を指定したデータ型に変換できるかどうかを判断します。 このバージョンのオーバーロードされたメソッドでは、カルチャに依存しない形式を使用して値をテストするかどうかを指定できます。

public:
 static bool CanConvert(System::String ^ text, System::Web::UI::WebControls::ValidationDataType type, bool cultureInvariant);
public static bool CanConvert(string text, System.Web.UI.WebControls.ValidationDataType type, bool cultureInvariant);
static member CanConvert : string * System.Web.UI.WebControls.ValidationDataType * bool -> bool
Public Shared Function CanConvert (text As String, type As ValidationDataType, cultureInvariant As Boolean) As Boolean

パラメーター

text
String

テストする文字列。

type
ValidationDataType

ValidationDataType列挙値の 1 つ。

cultureInvariant
Boolean

true カルチャに依存しない形式を使用して値をテストする場合。それ以外の場合は false

返品

true 指定したデータ文字列を指定したデータ型に変換できる場合は 、次の値を指定します。それ以外の場合は false

注釈

指定した文字列を指定したデータ型に変換できるかどうかを判断するには、 CanConvert(String, ValidationDataType, Boolean) メソッドを使用します。 このメソッドは、そのデータ型に依存する操作を実行する前に、文字列を互換性のあるデータ型に変換できるかどうかをテストするために一般的に使用されます。 カルチャに依存しない形式を使用して値をテストする必要があることを示すには、cultureInvariant パラメーターにtrueを渡します。それ以外の場合は、現在のカルチャで使用されている形式を使用して値がテストされます。 現在のカルチャで使用されている形式を使用して値をテストする場合は、このメソッドのオーバーロードされたバージョン BaseCompareValidator.CanConvert(String, ValidationDataType) 使用することを検討してください。

こちらもご覧ください

適用対象