TextBox.MaxLength TextBox.MaxLength TextBox.MaxLength TextBox.MaxLength Property

定義

取得或設定文字方塊中所允許的字元數上限。Gets or sets the maximum number of characters allowed in the text box.

public:
 virtual property int MaxLength { int get(); void set(int value); };
[System.Web.UI.Themeable(false)]
[System.ComponentModel.Bindable(true)]
public virtual int MaxLength { get; set; }
member this.MaxLength : int with get, set
Public Overridable Property MaxLength As Integer

屬性值

文字方塊中所允許的字元數上限。The maximum number of characters allowed in the text box. 預設值為 0,表示這個屬性未設定。The default is 0, which indicates that the property is not set.

例外狀況

範例

本主題提供具有原始程式碼的 Visual Studio 網站專案:下載A Visual Studio Web site project with source code is available to accompany this topic: Download.

下列程式碼範例示範如何使用TextBox控制項來取得使用者輸入。The following code example demonstrates how to use the TextBox control to get user input. 當使用者按一下 [新增] 按鈕時,就會顯示在文字方塊中輸入之值的總和。When the user clicks the Add button, the sum of the values entered in the text boxes is displayed.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ Page Language="C#" AutoEventWireup="True" %>  
  
<!DOCTYPE html>  
<html>   
  
<head>  
    <title>TextBox Example</title>  
    <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 id="form1" runat="server">  
    <h3> TextBox Example </h3>  
    <p>  
        <asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/>  
        +  
        <asp:TextBox ID="Value2" Columns="2" MaxLength="3" Text="1" runat="server"/>  
        =  
        <asp:Label ID="AnswerMessage" runat="server"/>  
    </p>  
    <p>  
        <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/>  
    </p>  
    <p>  
        <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1"  
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"  
             runat="server"/>  
        <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2"  
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"  
             runat="server"/>  
        <asp:RangeValidator ID="Value1RangeValidator" ControlToValidate="Value1"  
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"  
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"  
             runat="server"/>  
        <asp:RangeValidator ID="Value2RangeValidator" ControlToValidate="Value2"  
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"  
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"      
             runat="server"/>  
    </p>  
  </form>  
</body>  
</html>  
<%@ Page Language="vb" AutoEventWireup="false" %>  
  
<!DOCTYPE html>  
<html>   
  
<head>  
    <title>TextBox Example</title>  
    <script runat="server">  
        Protected Sub AddButton_Click(sender As Object, e As EventArgs)  
            Dim Answer As Integer  
            Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text)  
            AnswerMessage.Text = Answer.ToString()  
        End Sub  
    </script>  
</head>  
<body>  
  <form id="form1" runat="server">  
    <h3> TextBox Example </h3>  
    <p>  
        <asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/>  
        +  
        <asp:TextBox ID="Value2" Columns="2" MaxLength="3" Text="1" runat="server"/>  
        =  
        <asp:Label ID="AnswerMessage" runat="server"/>  
    </p>  
    <p>  
        <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/>  
    </p>  
    <p>  
        <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1"  
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"  
             runat="server"/>  
        <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2"  
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"  
             runat="server"/>  
        <asp:RangeValidator ID="Value1RangeValidator" ControlToValidate="Value1"  
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"  
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"  
             runat="server"/>  
        <asp:RangeValidator ID="Value2RangeValidator" ControlToValidate="Value2"  
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"  
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"      
             runat="server"/>  
    </p>  
  </form>  
</body>  
</html>  

備註

使用屬性來限制可TextBox在控制項中輸入的字元數。 MaxLengthUse the MaxLength property to limit the number of characters that can be entered in the TextBox control.

注意

只有當TextMode屬性設定為TextBoxMode.SingleLineTextBoxMode.Password時,這個屬性才適用。This property is applicable only when the TextMode property is set to TextBoxMode.SingleLine or TextBoxMode.Password.

這個屬性無法由佈景主題或樣式表主題設定。This property cannot be set by themes or style sheet themes. 如需詳細資訊, ThemeableAttribute請參閱和ASP.NET 主題和外觀For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

適用於

另請參閱