Udostępnij przez


CustomValidator.ClientValidationFunction Właściwość

Definicja

Pobiera lub ustawia nazwę niestandardowej funkcji skryptu po stronie klienta używanej do walidacji.

public:
 property System::String ^ ClientValidationFunction { System::String ^ get(); void set(System::String ^ value); };
public string ClientValidationFunction { get; set; }
[System.Web.UI.Themeable(false)]
public string ClientValidationFunction { get; set; }
member this.ClientValidationFunction : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.ClientValidationFunction : string with get, set
Public Property ClientValidationFunction As String

Wartość właściwości

Nazwa niestandardowej funkcji skryptu klienta używanej do walidacji. Wartość domyślna to Empty, która wskazuje, że ta właściwość nie jest ustawiona. Nazwa funkcji nie powinna zawierać żadnych nawiasów ani parametrów.

Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą ClientValidationFunction właściwości określić nazwę funkcji, która wykonuje walidację po stronie klienta. Funkcja walidacji sprawdza liczbę parzystą. Aby uzyskać opis parametrów funkcji, zobacz sekcję Uwagi w temacie CustomValidator.

Ważne

W tym przykładzie znajduje się pole tekstowe, które akceptuje dane wejściowe użytkownika, co jest potencjalnym zagrożeniem bezpieczeństwa. Domyślnie ASP.NET strony sieci Web weryfikują, czy dane wejściowe użytkownika nie zawierają skryptów ani elementów HTML. Aby uzyskać więcej informacji, zobacz Omówienie luk w zabezpieczeniach skryptów.

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e) 
      { 
         // Display whether the page passed validation.
         if (Page.IsValid) 
         {
            Message.Text = "Page is valid.";
         }

         else 
         {
            Message.Text = "Page is not valid!";
         }
      }

      void ServerValidation(object source, ServerValidateEventArgs args)
      {
         try 
         {
            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);
         }

         catch(Exception ex)
         {
            args.IsValid = false;
         }
      }
       
   </script>    

</head>
<body>

   <form id="Form1" runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
        

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           runat="server"/>

      <p>
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
</body>
</html>

<script language="javascript"> 
   function ClientValidate(source, arguments)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub ValidateBtn_OnClick(sender As Object, e As EventArgs) 
           ' Display whether the page passed validation.
           If Page.IsValid Then
               Message.Text = "Page is valid."
           Else
               Message.Text = "Page is not valid!"
           End If
       End Sub

      Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
           Try
               ' Test whether the value entered into the text box is even.
               Dim num As Integer = Integer.Parse(args.Value)
               args.IsValid = ((num Mod 2) = 0)
           Catch ex As Exception
               args.IsValid = False
           End Try
       End Sub

   </script>      

</head>
<body>

   <form id="Form1" runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
        

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           runat="server"/>

      <p>
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
  
</body>
</html>

<script language="javascript">
   function ClientValidate(source, arguments)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

Uwagi

Ustaw tę właściwość na nazwę funkcji, która wykonuje walidację po stronie klienta.

Ponieważ funkcja weryfikacji klienta jest uruchamiana w przeglądarce docelowej, funkcja musi być napisana przy użyciu języka skryptowego obsługiwanego przez przeglądarkę, takiego jak JScript lub VBScript.

Nie można ustawić tej właściwości według motywów ani motywów arkusza stylów. Aby uzyskać więcej informacji, zobacz ThemeableAttributei ASP.NET Motywy i skóry.

Dotyczy

Zobacz też