TextBox.MaxLength 属性

定义

获取或设置文本框中最多允许的字符数。

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

属性值

文本框中最多允许的字符数。 默认值为 0,表示未设置该属性。

属性

例外

指定的宽度小于 0。

示例

本主题附带了一个包含源代码的 Visual Studio 网站项目: 下载

下面的代码示例演示如何使用 TextBox 控件获取用户输入。 当用户单击“ 添加 ”按钮时,将显示文本框中输入的值的总和。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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>

注解

MaxLength使用 属性限制可在 控件中TextBox输入的字符数。

注意

仅当 属性设置为 TextBoxMode.SingleLineTextBoxMode.Password时,TextMode此属性才适用。

无法通过主题或样式表主题设置此属性。 有关详细信息,请参阅 ThemeableAttributeASP.NET 主题和外观

适用于

另请参阅