次の方法で共有


BaseCompareValidator.CanConvert メソッド

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

Public Shared Function CanConvert( _
   ByVal text As String, _   ByVal type As ValidationDataType _) As Boolean
[C#]
public static bool CanConvert(stringtext,ValidationDataTypetype);
[C++]
public: static bool CanConvert(String* text,ValidationDataTypetype);
[JScript]
public static function CanConvert(
   text : String,type : ValidationDataType) : Boolean;

パラメータ

戻り値

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

解説

CanConvert メソッドを使用して、指定した文字列を指定したデータ型に変換できるかどうかを確認します。このメソッドは、通常、データ型に依存する操作を実行する前に、文字列を互換性のある型に変換できるかどうかをテストするために使用されます。

メモ   このメソッドは、静的 (Visual Basic では Shared) であるため、クラス名と共にメソッド名を限定することにより、クラスのインスタンスを作成せずに使用できます。たとえば、 BaseCompareValidator.CanConvert などです。

使用例

 

<%@ 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] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

参照

BaseCompareValidator クラス | BaseCompareValidator メンバ | System.Web.UI.WebControls 名前空間 | ValidationDataType