TextBox.MaxLength屬性.

來源:互聯網
上載者:User

最近留戀於Csdn社區,發現很多網友問一些關於TextBox的MaxLength屬性的問題.現將SDK說明列於此,
TextBox.MaxLength 屬性
擷取或設定文字框中最多允許的字元數
文字框中最多允許的字元數。預設值為 0,表示未設定該屬性。
使用 MaxLength 屬性限定可以在 TextBox 控制項中輸入的字元數。

注意 :僅當 TextMode 屬性設定為 TextBoxMode.SingleLine 或 TextBoxMode.Password 時,此屬性才適用。
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">

      protected void AddButton_Click(Object sender, EventArgs e)
      {
         int Answer;

         Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text);

         AnswerMessage.Text = Answer.ToString();

      }

   </script>

</head>
<body>
   <form runat="server">
      <h3> TextBox 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>
               <asp:TextBox ID="Value1"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>
            </td>
            <td>
               + 
            </td>
            <td>
               <asp:TextBox ID="Value2"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>
            </td>
            <td>
               =
            </td>
            <td>            
               <asp:Label 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:RangeValidator
                    ID="Value1RangeValidator"
                    ControlToValidate="Value1"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br/> between than 1 and 100.<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:RangeValidator
                    ID="Value2RangeValidator"
                    ControlToValidate="Value2"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br/> between than 1 and 100.<br/>"
                    Display="Dynamic"
                    runat="server"/>

            </td>

            <td>

               &nbsp
 
            </td

         </tr>

         <tr align="center">

            <td colspan="4">

               <asp:Button ID="AddButton"
                    Text="Add"
                    OnClick="AddButton_Click"
                    runat="server"/>
            </td>
            <td>
               &nbsp;
            </td>
         </tr>
      </table>
   </form>
</body>
</html>
樣本展示如何使用 MaxLength 屬性將 TextBox 控制項允許的字元數限定為 3。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.