Introduction:
In this article I am going to share the trick to set the maximum number of
characters that can be entered in asp.net multiline textbox using both C# and
VB languages.
In previous articles i explained the examples like jQuery to limit and display number of characters left in asp.net multiline textbox and jQuery to Reset or Clear all Asp.Net or HTML controls placed on page and jQuery to expand or resize Asp.net multiline textbox on mouse over and mouse out and Auto resize textarea or Asp.net multiline textbox based on content using javascript and Disable browser autocomplete feature in TextBox
Description:
We all know that we can set the maximum allowed characters in normal textbox by
setting its MaxLength property to desired number of characters. But same
doesn’t work if the TextMode property of textbox is set to Multiline.
When
a Textbox is in SingleLine mode which is default
TextMode , it gets rendered as an input type textbox and when we set its TextMode property to MultiLine, it gets rendered as a textarea. As we know, MaxLength property works only for input
type and not for textarea. But There are multiple ways to handle this using
javascript, jquery or regular expression.
But I got the simple solution to handle this without using jquery or regularexpression.
Implementation:
Let’s create an example to demonstrate
this.
HTML Source
<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());
}
}
Asp.Net VB code to set max length
of multiline textbox
Protected Sub Page_Load(sender As Object,
e As EventArgs)
Handles
Me.Load
If (Not Page.IsPostBack) Then
txtComments.Attributes.Add("maxlength", txtComments.MaxLength.ToString())
End If
End Sub
"A
blog is nothing without reader's feedback and comments. So please
provide your valuable feedback so that i can make this blog better and If
you like my work; you can appreciate by leaving your comments, hitting
Facebook like button, following on Google+, Twitter, Linked in and
Pinterest, stumbling my posts on stumble upon and subscribing for
receiving free updates directly to your inbox . Stay tuned and stay
connected for more technical updates."
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..