共用方式為


HtmlInputButton.OnServerClick(EventArgs) 方法

定義

引發 ServerClick 事件。 這允許您直接處理事件。

protected:
 virtual void OnServerClick(EventArgs ^ e);
protected virtual void OnServerClick (EventArgs e);
abstract member OnServerClick : EventArgs -> unit
override this.OnServerClick : EventArgs -> unit
Protected Overridable Sub OnServerClick (e As EventArgs)

參數

e
EventArgs

EventArgs,其中包含事件資料。

範例

下列程式碼範例示範如何指定 和 撰寫 ServerClick 事件的處理常式程式碼。 事件處理常式會在頁面上新增兩個文字方塊的值,並顯示結果。

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

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

<html xmlns="http://www.w3.org/1999/xhtml" > 

<head>
    <title> HtmlInputButton Example </title>
</head>
<body>
<form id="form1" runat="server">
    <div>

    <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 a 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 a 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"
                      id="SubmitButton"
                      value="Add"
                      onserverclick="AddButton_Click"
                      runat="server"/>

                  

               <input type="Reset"
                      id="ResetButton"
                      value="Reset"
                      runat="server"/>

            </td>
            <td>
                 
            </td>
        </tr>
    </table>

    </div>
</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">

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

<html xmlns="http://www.w3.org/1999/xhtml" > 

<head>
    <title> HtmlInputButton Example </title>
</head>
<body>
<form id="form1" runat="server">
    <div>

    <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 a 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 a 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"
                      id="SubmitButton"
                      value="Add"
                      onserverclick="AddButton_Click"
                      runat="server"/>

                  

               <input type="Reset"
                      id="ResetButton"
                      value="Reset"
                      runat="server"/>

            </td>
            <td>
                 
            </td>
        </tr>
    </table>

    </div>
</form>
</body>
</html>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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 HtmlInputButton 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 HtmlInputButton 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>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HtmlInputButton ServerClick Example</title>
</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlInputButton ServerClick Example </h3>

      <asp:PlaceHolder ID="ControlContainer"
           runat="server"/>

      <br /><br />
 
      <span id="Message"
            runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create a new HtmlInputButton control.
    Dim NewButtonControl As New HtmlInputButton()

    ' Set the properties of the new HtmlInputButton 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 HtmlInputButton control to the Controls collection of the
    ' PlaceHolder control. 
    ControlContainer.Controls.Add(NewButtonControl)

  End Sub


  Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)

    ' Display a simple message. 
    Message.InnerHtml = "Thank you for clicking the button."

  End Sub

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HtmlInputButton ServerClick Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <h3> HtmlInputButton ServerClick Example </h3>

      <asp:PlaceHolder ID="ControlContainer"
           runat="server"/>

      <br /><br />
 
      <span id="Message"
            runat="server"/>

   </form>

</body>
</html>

備註

按一下控制項時 HtmlInputButton ,就會 ServerClick 引發 事件。

注意

[重設] 按鈕不會引發 ServerClick 事件。

當您建立 HtmlInputButton 委派 (Delegate) 時,就可以識別即將處理此事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱