Multiline TextBox Max Length in ASP.Net

TextBox MaxLength Property – ASP.Net

In our previous asp.net tutorial we got idea about TextBox control in asp.net and overview of TextBox properties. Now in this post we will learn TextBox MaxLength property  in detail.

TextBox MaxLength

MaxLength property is used to set maximum number of characters  user can input in textbox control. The MaxLength property only works with Singleline textbox, it doesn’t work with Multiline textbox. We will see MaxLength property with both Singleline textbox and Multiline textbox.

SingleLine TextBox MaxLength Example

In .net by default TextMod=SingleLine for a normal textbox, it displays as single line textbox.

Step 1 – Open the Visual Studio –> Create a new empty Web application.

Step 2 – Create a New web page for TextBox MaxLength example.

Step 3 –Drag and drop TextBox control on web page from Toolbox.

Step 4 – Set MaxLenght = 10 for all only input 10 character in control.

Set TextBox MaxLength in ASP.Net
Set TextBox MaxLength in ASP.Net

<asp:TextBox ID=”TextBox1″ runat=”server” MaxLength=”10″ ></asp:TextBox>

Above figure we set MaxLenght = 10 manually so it  is fixes for that textbox, but we want to assign dynamically MaxLength property to control then we need to do programming in c# language for set MaxLength property of TextBox control.

Here, we take an example for set MaxLength = 10, MaxLength = 6 and default textbox length in asp.net.

Set TextBox MaxLength in ASP.Net
Set TextBox MaxLength Example in ASP.Net

C# code for above example

public partial class TextBox_MaxLength : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btn10_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox1.MaxLength = 10;
        Label1.Text = "Set MaxLength = 10";
    }
    protected void btn6_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox1.MaxLength = 6;
        Label1.Text = "Set MaxLength = 6";
    }
    protected void btndefault_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox1.MaxLength = 0;
        Label1.Text = "Set default Textbox length";
    }
}

MultiLine TextBox MaxLength Example

For multiline textbox set TextMode = “MultiLine” and set MaxLength = 6 and we need to  write some c# code on page_load events of web form.

write below c# code in page_load events.

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("maxlength", TextBox1.MaxLength.ToString());
}

Set MaxLength on MultiLine TextBox

Set MeltiLine TextBox MaxLength Example in ASP.Net
Set MeltiLine TextBox MaxLength Example in ASP.Net

MultiLine MaxLength ASP.Net Example

Set MeltiLine TextBox MaxLength Example in ASP.Net
Set MeltiLine TextBox MaxLength Example in ASP.Net

write below c# code for above example.

protected void btn10_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox1.MaxLength = 10;
        TextBox1.Attributes.Add("maxlength", TextBox1.MaxLength.ToString());
        Label1.Text = "Set MaxLength = 10";
    }
    protected void btn6_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox1.MaxLength = 6;
        TextBox1.Attributes.Add("maxlength", TextBox1.MaxLength.ToString());
        Label1.Text = "Set MaxLength = 6";
    }