System Information

Impossible Is Nothing...

导航

TextBox.MaxLength属性.

最近留恋于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。

posted on 2005-07-05 14:22  SysInfo  阅读(3243)  评论(3编辑  收藏  举报