BaseCompareValidator.CanConvert Methode

Definitie

Bepaalt of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype.

Overloads

Name Description
CanConvert(String, ValidationDataType)

Bepaalt of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype. In deze versie van de overbelaste methode worden valuta-, dubbele en datumwaarden getest met behulp van de notatie die wordt gebruikt door de huidige cultuur.

CanConvert(String, ValidationDataType, Boolean)

Bepaalt of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype. Met deze versie van de overbelaste methode kunt u opgeven of waarden worden getest met een cultuurneutrale indeling.

CanConvert(String, ValidationDataType)

Bepaalt of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype. In deze versie van de overbelaste methode worden valuta-, dubbele en datumwaarden getest met behulp van de notatie die wordt gebruikt door de huidige cultuur.

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

Parameters

text
String

De tekenreeks die moet worden getest.

type
ValidationDataType

Een van de ValidationDataType waarden.

Retouren

true als de opgegeven gegevensreeks kan worden geconverteerd naar het opgegeven gegevenstype; anders, false.

Voorbeelden

In het volgende voorbeeld ziet u hoe u de CanConvert methode gebruikt om twee gehele getallen te vergelijken en te bepalen of het tweede gehele getal kan worden geconverteerd.


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

Opmerkingen

Gebruik de CanConvert(String, ValidationDataType) methode om te bepalen of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype. Deze methode wordt vaak gebruikt om te testen of een tekenreeks kan worden geconverteerd naar een compatibel gegevenstype voordat een bewerking wordt uitgevoerd die afhankelijk is van dat gegevenstype.

Deze versie van de methode test de waarde met behulp van de indeling die wordt gebruikt door de huidige cultuur. Als u de waarde wilt testen met een cultuurneutrale indeling, gebruikt u de BaseCompareValidator.CanConvert(String, ValidationDataType, Boolean) overbelaste versie van deze methode.

Zie ook

Van toepassing op

CanConvert(String, ValidationDataType, Boolean)

Bepaalt of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype. Met deze versie van de overbelaste methode kunt u opgeven of waarden worden getest met een cultuurneutrale indeling.

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

Parameters

text
String

De tekenreeks die moet worden getest.

type
ValidationDataType

Een van de ValidationDataType opsommingswaarden.

cultureInvariant
Boolean

true om waarden te testen met behulp van een cultuurneutrale indeling; anders, false.

Retouren

true als de opgegeven gegevensreeks kan worden geconverteerd naar het opgegeven gegevenstype; anders, false.

Opmerkingen

Gebruik de CanConvert(String, ValidationDataType, Boolean) methode om te bepalen of de opgegeven tekenreeks kan worden geconverteerd naar het opgegeven gegevenstype. Deze methode wordt vaak gebruikt om te testen of een tekenreeks kan worden geconverteerd naar een compatibel gegevenstype voordat een bewerking wordt uitgevoerd die afhankelijk is van dat gegevenstype. Als u wilt aangeven dat waarden moeten worden getest met een cultuurneutrale notatie, geeft u de cultureInvariant parameter doortrue. Anders worden waarden getest met behulp van de indeling die door de huidige cultuur wordt gebruikt. Wanneer u een waarde test met behulp van de indeling die wordt gebruikt door de huidige cultuur, kunt u overwegen de BaseCompareValidator.CanConvert(String, ValidationDataType) overbelaste versie van deze methode te gebruiken.

Zie ook

Van toepassing op