Share via


MaxLength Property

Returns the maximum number of characters allowed in the text box. The default value is 0, meaning that there is no limit on the length.

public int MaxLength {
   get,
   set
}

Example

The following example demonstrates how to use the MaxLength property to limit the number of characters that can be written in a TextBox control.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
   txtUserName.MaxLength = 9
   txtUserPwd2.MaxLength = 7

End If

End Sub

[C#]

void Page_Load(Object sender, EventArgs e)
{
   if(!IsPostBack)
   {
     txtUserName.MaxLength = 9;
     txtUserPwd2.MaxLength = 7;
   }
}

See Also

Applies to: TextBox Class