共用方式為


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

更新:2007 年 11 月

建立伺服器端控制項,使其對應至 <input type=text> 和 <input type=password> HTML 項目,並允許您建立單行文字方塊來接收使用者輸入。

 <input      Type="Password|Text"     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"     OnServerChange="OnServerChange event handler"     OnUnload="OnUnload event handler"     runat="server"     />

備註

您可以使用 HtmlInputText 控制項,針對 <input type=text> 和 <input type=password> HTML 項目執行伺服器程式碼。如同使用標準 HTML,這些控制項可用來以 HTML 格式輸入使用者名稱和密碼。

注意事項:

當 Type 屬性設為 password 時,在文字方塊中會以遮罩方式顯示您的輸入。

您可以搭配 HtmlInputButtonHtmlInputImageHtmlButton 控制項使用這個控制項,以便在伺服器上處理使用者輸入。您可以分別使用 MaxLengthSizeValue 屬性,來控制可以輸入的字元數、寬度和控制項的內容。

注意事項:

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

範例

下列範例會示範如何使用 HtmlInputText 控制項取得使用者輸入。

<%@ 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> HtmlInputText Example </title>
</head>
<body>
<form id="form1" runat="server">
    <div>

    <h3> HtmlInputText 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 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>
                &nbsp;
            </td>
        </tr>
        <tr align="center">
            <td colspan="4">
                <input type="Submit"
                      name="AddButton"
                      value="Add"
                      onserverclick="AddButton_Click"
                      runat="server"/>
                &nbsp;&nbsp;&nbsp;
                <input 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> HtmlInputText Example </title>
</head>
<body>
<form id="form1" runat="server">
    <div>

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


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

請參閱

參考

HtmlInputText

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

HtmlInputControl

其他資源

HTML 伺服器控制項