ServerValidateEventHandler Delegate
Represents the method that will handle the ServerValidate event of a CustomValidator control.
[Visual Basic] <Serializable> Public Delegate Sub ServerValidateEventHandler( _ ByVal source As Object, _ ByVal args As ServerValidateEventArgs _ ) [C#] [Serializable] public delegate void ServerValidateEventHandler( object source, ServerValidateEventArgs args ); [C++] [Serializable] public __gc __delegate void ServerValidateEventHandler( Object* source, ServerValidateEventArgs* args );
[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.
Parameters [Visual Basic, C#, C++]
The declaration of your event handler must have the same parameters as the ServerValidateEventHandler delegate declaration.
- source
- The source of the event.
- args
- A ServerValidateEventArgs that contains the event data.
Remarks
Use this delegate to provide custom code that performs validation on the server. Your code needs to validate the Value property of the args parameter passed to the delegate. Store the result of the validation in the IsValid property of the args parameter.
When you create a ServerValidateEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.
For more information about handling events, see Consuming Events.
Example
[Visual Basic, C#] The following example demonstrates how to specify and code a handler for a server-side CustomValidator. It displays whether a number entered into a text box is odd or even.
[Visual Basic] <%@ 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 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" Display="Static" ErrorMessage="Not an even number!" ForeColor="green" Font-Name="verdana" Font-Size="10pt" OnServerValidate="ServerValidation" runat="server"/> <p> <asp:Button id="Button1" Text="Validate" OnClick="ValidateBtn_OnClick" runat="server"/> </form> </body> </html> [C#] <%@ 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 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" Display="Static" ErrorMessage="Not an even number!" ForeColor="green" Font-Name="verdana" Font-Size="10pt" OnServerValidate="ServerValidation" runat="server"/> <p> <asp:Button id="Button1" Text="Validate" OnClick="ValidateBtn_OnClick" runat="server"/> </form> </body> </html> [Visual Basic] <%@ 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 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 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" 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> [C#] <%@ 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; } } 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 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" 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>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI.WebControls
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)