BaseCompareValidator.CanConvert 方法

定義

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

多載

名稱 Description
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 價值。

傳回

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

傳回

true若指定的資料字串能轉換為指定的資料型態;否則,。 false

備註

使用此 CanConvert(String, ValidationDataType, Boolean) 方法判斷指定的字串是否能轉換為指定的資料型態。 此方法常用於測試字串是否能轉換為相容的資料型態,然後再執行依賴該資料型態的操作。 若要表示應使用文化中性格式測試數值,輸入truecultureInvariant參數;否則,則使用當前文化所用格式進行測試。 在使用當前文化格式測試值時,建議使用 BaseCompareValidator.CanConvert(String, ValidationDataType) 此方法的超載版本。

另請參閱

適用於