共用方式為


HtmlInputButton 伺服器控制項宣告式語法

建立伺服器端控制項,使其對應至 <input type=button>、<input type=submit> 和 <input type=reset> HTML 項目,並允許您分別建立命令按鈕、送出按鈕或重設按鈕。

<input
    Type="Button|Reset|Submit"
    EnableViewState="False|True"
    Id="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnServerClick="OnServerClick event handler"
    OnUnload="OnUnload event handler"
    runat="server"
    />

備註

您可以使用 HtmlInputButton 控制項,對 <input type=button>、<input type=submit> 和 <input type=reset> HTML 項目進行設計程式。 當使用者按一下 HtmlInputButton 控制項時,控制項內嵌之表單上的輸入,就會張貼到伺服器並進行處理。 然後,回應會被傳回至要求瀏覽器。

您可以為 ServerClick 事件提供自訂事件處理常式,以便在按一下控制項時,執行一組特定的指令。

注意事項注意事項

reset 按鈕不支援 ServerClick 事件。當按一下 reset 按鈕時,不一定會清除網頁上的所有輸入控制項。相反地,當載入網頁時,它們會回復其原始狀態。例如,如果文字方塊原先包含 "JohnDoe" 值,按一下 reset 按鈕就會將文字方塊回復為這個值。

搭配使用 HtmlInputTextHtmlTextArea 控制項時,您就可以建立能在伺服器上處理的使用者輸入或驗證 (Authentication) 網頁。

注意事項注意事項

這個控制項不需要結尾標記。

範例

下列範例會示範如何使用 HtmlInputButton 控制項將表單送出至伺服器進行處理。

<%@ 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(ByVal sender As Object, ByVal 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">
                 &nbsp;
             </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>
                 &nbsp;
             </td>
         </tr>
         <tr align="center">
             <td colspan="4">
                 <input id="Submit1" type="Submit"
                     name="AddButton"
                     value="Add"
                     onserverclick="AddButton_Click"
                     runat="server" />

                 &nbsp;&nbsp;&nbsp;

                 <input id="Reset1" type="Reset"
                     name="AddButton"
                     value="Reset"
                     runat="server"/>
             </td>
             <td>
                 &nbsp;
            </td>
        </tr>
    </table>

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

<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">
                 &nbsp;
             </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>
                 &nbsp;
             </td>
         </tr>
         <tr align="center">
             <td colspan="4">
                 <input id="Submit1" type="Submit"
                     name="AddButton"
                     value="Add"
                     onserverclick="AddButton_Click"
                     runat="server" />

                 &nbsp;&nbsp;&nbsp;

                 <input id="Reset1" type="Reset"
                     name="AddButton"
                     value="Reset"
                     runat="server"/>
             </td>
             <td>
                 &nbsp;
            </td>
        </tr>
    </table>

    </div>
</form>
</body>
</html>

請參閱

工作

HOW TO:使用 ASP.NET 語法加入 HTML 伺服器控制項至 Web 網頁

參考

HtmlInputButton