ServerValidateEventHandler Delegát

Definice

Představuje metodu, která bude zpracovávat ServerValidate událost CustomValidator ovládacího prvku.

public delegate void ServerValidateEventHandler(System::Object ^ source, ServerValidateEventArgs ^ args);
public delegate void ServerValidateEventHandler(object source, ServerValidateEventArgs args);
type ServerValidateEventHandler = delegate of obj * ServerValidateEventArgs -> unit
Public Delegate Sub ServerValidateEventHandler(source As Object, args As ServerValidateEventArgs)

Parametry

source
Object

Zdroj události

args
ServerValidateEventArgs

A ServerValidateEventArgs , který obsahuje data události.

Příklady

Následující příklad ukazuje, jak zadat a kódovat obslužnou rutinu pro server na straně CustomValidator. Zobrazuje, jestli je číslo zadané do textového pole liché nebo sudé.


<%@ 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>
    <title>CustomValidator ServerValidate Example</title>
<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-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1"/>

      <br />

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

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

      <br />
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           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>
    <title>CustomValidator ServerValidate Example</title>
<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-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1" />

      <br />

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

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

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

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

<%@ 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>
    <title>CustomValidator ServerValidate Example</title>
<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;

         }

      }

      void Page_Load(object sender, EventArgs e)
      {

         // Manually register the event-handling method for the  
         // ServerValidate event of the CustomValidator control.
         CustomValidator1.ServerValidate += 
             new ServerValidateEventHandler(this.ServerValidation);

      }

   </script>    

</head>
<body>

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

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

      <br />

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

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

      <br />
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           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>
    <title>CustomValidator ServerValidate Example</title>
<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

      Sub Page_Load(sende As object, e As EventArgs)

         ' Manually register the event-handling method for the  
         ' ServerValidate event of the CustomValidator control.
         AddHandler CustomValidator1.ServerValidate, _
             AddressOf ServerValidation

      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-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1" />

      <br />

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

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

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

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

Poznámky

Pomocí tohoto delegáta můžete zadat vlastní kód, který provádí ověření na serveru. Váš kód musí ověřit Value vlastnost parametru args předaného delegátu. Výsledek ověření uložte do IsValid vlastnosti parametru args .

Při vytváření delegáta ServerValidateEventHandler identifikujete metodu, která bude událost zpracovávat. Pokud chcete událost přidružit k obslužné rutině události, přidejte do události instanci delegáta. Obslužná rutina události je volána při každém výskytu události, dokud neodeberete delegáta. Další informace o delegátech obslužné rutiny událostí najdete v tématu Zpracování a vyvolávání událostí.

Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Metody rozšíření

GetMethodInfo(Delegate)

Získá objekt, který představuje metodu reprezentovanou zadaným delegátem.

Platí pro

Viz také