Set MaxLength property of asp.net multiline textbox

How to set MaxLength property of asp.net multiline textbox:


We all apprehend that we are able to set the utmost allowed characters in traditional textbox by setting its MaxLength property to desired range of characters. however same doesn’t work if the TextMode property of textbox is ready to Multiline.

When a Textbox is in SingleLine mode that is default TextMode , it gets rendered as AN input kind textbox  and after we set its TextMode property  to MultiLine, it gets rendered as a textarea. As we know, MaxLength property works just for input kind and not for textarea. however There square measure multiple ways in which to handle this victimization javascript,  jquery or regular expression. however I got the straightforward answer to handle this while not victimization jquery or regularexpression.


HTML Source for How to set MaxLength property of asp.net

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Set Max Length of multiline textbox </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <fieldset style="width:300px;">
           <legend>Set max length of multiline textbox</legend>
           <asp:TextBox ID="txtComments" runat="server" Width="300px" TextMode="MultiLine"MaxLength="60" placeholder="Max allowed:60 characters"></asp:TextBox>
       </fieldset>       
    </div>
    </form>
</body>
</html>

Asp.Net C# code to set max length of multiline textbox:


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtComments.Attributes.Add("maxlength", txtComments.MaxLength.ToString());
        }
    } 



Comments

Popular posts from this blog