BaseCompareValidator.CanConvert 方法

定義

判斷指定的字串是否可轉換成指定的資料型別。

多載

CanConvert(String, ValidationDataType)

判斷指定的字串是否可轉換成指定的資料型別。 這個版本的多載方法使用目前文化特性所使用的格式來測試貨幣值、雙精度浮點數值和日期值。

CanConvert(String, ValidationDataType, Boolean)

判斷指定的字串是否可轉換成指定的資料型別。 這個版本的多載方法可讓您指定是否使用文化特性中性的格式來測試值。

CanConvert(String, ValidationDataType)

判斷指定的字串是否可轉換成指定的資料型別。 這個版本的多載方法使用目前文化特性所使用的格式來測試貨幣值、雙精度浮點數值和日期值。

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

要測試的字串。

type
ValidationDataType

其中一個 ValidationDataType 值。

傳回

Boolean

如果指定的資料型別可以轉換成指定的資料型別,則為 true,否則為 false

範例

下列範例示範如何使用 CanConvert 方法來比較兩個整數值,並判斷是否可以轉換第二個整數。


<%@ 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 列舉值。

cultureInvariant
Boolean

true 表示使用文化特性中性的格式來測試值,否則為 false

傳回

Boolean

如果指定的資料型別可以轉換成指定的資料型別,則為 true,否則為 false

備註

CanConvert(String, ValidationDataType, Boolean)使用 方法來判斷指定的字串是否可以轉換成指定的資料類型。 這個方法通常用來測試字串是否可以轉換成相容的資料類型,然後再執行相依于該資料類型的作業。 若要指出應該使用文化特性中性格式來測試值,請傳入 true 參數 cultureInvariant ;否則,會使用目前文化特性所使用的格式來測試值。 使用目前文化特性所使用的格式測試值時,請考慮使用 BaseCompareValidator.CanConvert(String, ValidationDataType) 此方法的多載版本。

另請參閱

適用於