.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

The Easiest way to set the MaxLength property of a multiline texbox control in ASP.Net

Posted By:ASPEvil       Posted Date: June 15, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Setting "MaxLength" property of an ASP.Net multiline texbox control

Hi all.

Now a days, web applications are becoming more visible and small to smaller businesses are coming to the web world with at least one simple, static website, which express their online existences and as websites are becoming more popular in the world, many companies and businessmen are trying to establish their own business websites as well as personal websites are also being made day by date and for that, we are able to see many new and exciting websites are being launched in the Internet, daily.

And as software professionals, we (you and me) are so much interested to create websites like these exciting websites and if we will get or if we are getting the chances to make websites, like these beautiful websites, then we will use or are using our own ASP.Net, no doubt.

But there is a big problem sleeping in ASP.Net website creation, that is, whenever we try to use a textbox control to take inputs from the users of the websites, we create, we occasionally set textbox's property to "MultiLine" as well as we want the "MaxLength" property to work for this MultiLine texbox control. But alas, it does not work.

Why MultiLine textbox control's MaxLength property does not work?
Ans:- Whenever we run our ASP.Net websites, this MultiLine textbox control is converted to the TextArea control of HTML and as the HTML's TextArea control does not have any MaxLength property, so this ASP.Net's MultiLine textbox control also ignores its MaxLength property at runtime and simply does not work and users can insert just any number characters, they want, regardless of the maximum number we had set for them to insert.

I tried a lot of code to resolve this issue and after a lot of work and study I did follow few steps to resolve this issue, as written below:-

1) Drag&Drop a RegularExpressionValidator control on the ASP.Net design page, from the toolbox.
2) Drag&Drop a textbox from the toolbox, whose property is going to be MultiLine.
3) Set the textbox's TextMode to MultiLine.(Don't need to set the MaxLength property of this textbox control).
4) Now do a single click on the RegularExpressionValidator control, on the design page and go to its property page by pressing F4.
5) In the property's "Behavior" section, set the "ControlToValidate" property of this RegularExpressionValidator control to the textbox(select the textbox's ID), which is a MultiLine textbox control.
6) Now, go to the "ValidationExpression" of RegularExpressionValidator control and type an expression as ^.{0,200}$

I tried the above step not to allow users to enter more than 200 characters in the MultiLine textbox control and it did work. So, in the above smallest expression, 200 is the MaxLength of the MultiLine textbox control. So, if you are wanting to reset the MaxLength of the MultiLine textbox control with a different value, then feel free to reset the 200(only this 200, don't change other parts of this expression, else it will not work) to the MaxLength you want; and believe, it will work just nicely.

So, just try it now and do your website making without any headache, but make your website more attractive and featured rich,ok? Who knows? I might be one of your website users, in very near future!

Happy Coding and website Development
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend