HtmlInputButton.OnServerClick Method
Raises the ServerClick event. This allows you to handle the event directly.
[Visual Basic] Protected Overridable Sub OnServerClick( _ ByVal e As EventArgs _ ) [C#] protected virtual void OnServerClick( EventArgs e ); [C++] protected: virtual void OnServerClick( EventArgs* e ); [JScript] protected function OnServerClick( e : EventArgs );
Parameters
- e
- A System.EventArgs that contains the event data.
Remarks
The ServerClick event is raised when an HtmlInputButton control is clicked.
Note A Reset button does not raise the ServerClick event.
When you create an HtmlInputButton 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.
Example
[Visual Basic, C#] The following example demonstrates how to specify and code a handler for the ServerClick event. The event handler adds the values of the two text boxes on the page and displays the results.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Protected Sub AddButton_Click(sender As Object, e As EventArgs) Dim Answer As Integer Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value) AnswerMessage.InnerHtml = Answer.ToString() End Sub </script> </head> <body> <form runat="server"> <h3> HtmlInputButton Example </h3> <table> <tr> <td colspan="5"> Enter integer values into the text boxes. <br> Click the Add button to add the two values. <br> Click the Reset button to reset the text boxes. </td> </tr> <tr> <td colspan="5"> </td> </tr> <tr align="center"> <td> <input ID="Value1" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> + </td> <td> <input ID="Value2" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> = </td> <td> <span ID="AnswerMessage" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MinCompareValidator" ControlToValidate="Value1" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MaxCompareValidator" ControlToValidate="Value1" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td colspan="2"> <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MinCompareValidator" ControlToValidate="Value2" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MaxCompareValidator" ControlToValidate="Value2" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td>   </td </tr> <tr align="center"> <td colspan="4"> <input Type="Submit" Name="AddButton Value="Add" OnServerClick="AddButton_Click" runat="server"/> <input Type="Reset" Name="AddButton Value="Reset" runat="server"/> </td> <td> </td> </tr> </table> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> protected void AddButton_Click(Object sender, EventArgs e) { int Answer; Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value); AnswerMessage.InnerHtml = Answer.ToString(); } </script> </head> <body> <form runat="server"> <h3> HtmlInputButton Example </h3> <table> <tr> <td colspan="5"> Enter integer values into the text boxes. <br> Click the Add button to add the two values. <br> Click the Reset button to reset the text boxes. </td> </tr> <tr> <td colspan="5"> </td> </tr> <tr align="center"> <td> <input ID="Value1" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> + </td> <td> <input ID="Value2" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> = </td> <td> <span ID="AnswerMessage" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MinCompareValidator" ControlToValidate="Value1" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MaxCompareValidator" ControlToValidate="Value1" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td colspan="2"> <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MinCompareValidator" ControlToValidate="Value2" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MaxCompareValidator" ControlToValidate="Value2" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td>   </td </tr> <tr align="center"> <td colspan="4"> <input Type="Submit" Name="AddButton Value="Add" OnServerClick="AddButton_Click" runat="server"/> <input Type="Reset" Name="AddButton Value="Reset" runat="server"/> </td> <td> </td> </tr> </table> </form> </body> </html> [Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) ' Create a new HtmlInputButton control. Dim NewButtonControl As New HtmlInputButton() ' Set the properties of the new HtmlButton control. NewButtonControl.ID = "NewButtonControl" NewButtonControl.Value = "Click Me" ' Create an EventHandler delegate for the method you want to handle the event ' and then add it to the list of methods called when the event is raised. AddHandler NewButtonControl.ServerClick, AddressOf Button_Click ' Add the new HtmlAnchor control to the Controls collection of the ' PlaceHolder control. ControlContainer.Controls.Add(NewButtonControl) End Sub Sub Button_Click(sender As Object, e As EventArgs) ' Display a simple message. Message.InnerHtml = "Thank you for clicking the button." End Sub </script> </head> <body> <form runat="server"> <h3> HtmlInputButton ServerClick Example </h3> <asp:PlaceHolder ID="ControlContainer" runat="server"/> <br><br> <span ID="Message" runat="server"/> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // Create a new HtmlInputButton control. HtmlInputButton NewButtonControl = new HtmlInputButton("submit"); // Set the properties of the new HtmlButton control. NewButtonControl.ID = "NewButtonControl"; NewButtonControl.Value = "Click Me"; // Create an EventHandler delegate for the method you want to handle the event // and then add it to the list of methods called when the event is raised. NewButtonControl.ServerClick += new System.EventHandler(this.Button_Click); // Add the new HtmlAnchor control to the Controls collection of the // PlaceHolder control. ControlContainer.Controls.Add(NewButtonControl); } void Button_Click(Object sender, EventArgs e) { // Display a simple message. Message.InnerHtml = "Thank you for clicking the button."; } </script> </head> <body> <form runat="server"> <h3> HtmlInputButton ServerClick Example </h3> <asp:PlaceHolder ID="ControlContainer" runat="server"/> <br><br> <span ID="Message" 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 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
HtmlInputButton Class | HtmlInputButton Members | System.Web.UI.HtmlControls Namespace | ServerClick | System.EventArgs