CustomValidator.OnServerValidate Method
Raises the ServerValidate event for the CustomValidator control.
[Visual Basic] Protected Overridable Function OnServerValidate( _ ByVal value As String _ ) As Boolean [C#] protected virtual bool OnServerValidate( string value ); [C++] protected: virtual bool OnServerValidate( String* value ); [JScript] protected function OnServerValidate( value : String ) : Boolean;
Parameters
- value
- The value to validate.
Return Value
true if the value specified by the value parameter passes validation; otherwise, false.
Remarks
The ServerValidate event is raised when validation is performed on the server.
Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.
The OnServerValidate method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
Notes to Inheritors: When overriding OnServerValidate in a derived class, be sure to call the base class's OnServerValidate method so that registered delegates receive the event.
Example
[Visual Basic, C#] The following example demonstrates how to specify and code a handler for a server-side CustomValidator.
[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
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
CustomValidator Class | CustomValidator Members | System.Web.UI.WebControls Namespace | ServerValidate