ServerValidateEventHandler 委托

定义

表示将处理 ServerValidate 控件的 CustomValidator 事件的方法。

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)

参数

source
Object

事件源。

args
ServerValidateEventArgs

包含事件数据的 ServerValidateEventArgs

示例

以下示例演示如何为服务器端 CustomValidator指定处理程序并为其编写代码。 它显示输入到文本框中的数字是奇数还是偶数。


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

注解

使用此委托提供在服务器上执行验证的自定义代码。 代码需要验证 Value 传递给委托的参数 args 的属性。 将验证结果存储在 参数的 属性中IsValidargs

创建 ServerValidateEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

有关如何处理事件的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

另请参阅