TextBox.MaxLength Property
Assembly: System.Web (in system.web.dll)
/** @property */ public int get_MaxLength () /** @property */ public void set_MaxLength (int value)
public function get MaxLength () : int public function set MaxLength (value : int)
Property Value
The maximum number of characters allowed in the text box. The default is 0, which indicates that the property is not set.Use the MaxLength property to limit the number of characters that can be entered in the TextBox control.
Note |
|---|
| 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. For more information, see ThemeableAttribute and ASP.NET Themes and Skins Overview.
| Topic | Location |
|---|---|
| How to: Set ASP.NET Web Server Control Properties | Building ASP .NET Web Applications |
| How to: Set ASP.NET Web Server Control Properties | Building ASP .NET Web Applications |
The following code example demonstrates how to use the MaxLength property to limit the number of characters allowed in the TextBox control to 3.
<%@ 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"> </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>   </td </tr> <tr align="center"> <td colspan="4"> <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/> </td> <td> </td> </tr> </table> </form> </body> </html>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note