Monday, August 13, 2007

Create TextArea control with working Maxlength property

We have found that text area (Multiline textbox) control in asp.net doesn't support the multiline property even you define that property.

so here is the solution for this problem

Code in .net Page

protected override void OnPreRender(EventArgs e)
{

if (MaxLength > 0 && TextMode == TextBoxMode.MultiLine)
{

// Add javascript handlers for paste and keypress
Attributes.Add("onkeypress", "doKeypress(this);");
Attributes.Add("onbeforepaste", "doBeforePaste(this);");
Attributes.Add("onpaste", "doPaste(this);");

// Add attribute for access of maxlength property on client-side
Attributes.Add("maxLength", this.MaxLength.ToString());

// Register client side include - only once per page

}
}

Code in JavaScript file

// Keep user from entering more than maxLength characters
function doKeypress(control)
{

maxLength = control.attributes["maxLength"].value;

value = control.value;

if(maxLength && value.length > maxLength-1)
{

event.returnValue = false;

maxLength = parseInt(maxLength);

}
}

// Cancel default behavior
function doBeforePaste(control)
{

maxLength = control.attributes["maxLength"].value;

if(maxLength)
{

event.returnValue = false;

}
}

// Cancel default behavior and create a new paste routine
function doPaste(control)
{

maxLength = control.attributes["maxLength"].value;

value = control.value;

if(maxLength)
{

event.returnValue = false;

maxLength = parseInt(maxLength);

var oTR = control.document.selection.createRange();

var iInsertLength = maxLength - value.length + oTR.text.length;

var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);

oTR.text = sData;

}
}


using this code you can even check the copy paste and restrict the number of characters

now create a custom control and then use that control

set MaxLenght property of that control and you will be able to restrict the number of characters

27 comments:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Celular, I hope you enjoy. The address is http://telefone-celular-brasil.blogspot.com. A hug.

Anonymous said...

Thanks in favor of sharing such a pleasant opinion, article
is fastidious, thats why i have read it fully

Also visit my site :: Las Vegas Golf Schools

Anonymous said...

I pay a visit each day a few web pages and information
sites to read content, except this blog presents quality based writing.


Feel free to surf to my website - short game instruction

Anonymous said...

First off I want to say awesome blog! I had a quick question that I'd like to ask if you don't mind.
I was interested to know how you center yourself and clear your head before writing.
I've had a difficult time clearing my mind in getting my thoughts out. I truly do enjoy writing but it just seems like the first 10 to 15 minutes are generally lost simply just trying to figure out how to begin. Any ideas or hints? Thanks!

Have a look at my weblog - best restaurants in columbia sc

Anonymous said...

Does your site have a contact page? I'm having a tough time locating it but, I'd
like to shoot you an e-mail. I've got some suggestions for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time.

Also visit my web blog; St Cloud Floral

Anonymous said...

These are truly wonderful ideas in regarding blogging. You have touched some fastidious factors here.
Any way keep up wrinting.

Also visit my website :: types of back surgery for bulging discs

Anonymous said...

Every weekend i used to go to see this web site, because i wish for enjoyment, for the reason that this this website conations truly good funny stuff too.


My blog utility sink

Anonymous said...

Hi there! This is my first visit to your blog! We are
a team of volunteers and starting a new initiative in a community in the same niche.
Your blog provided us useful information to work on. You have done a marvellous job!


My weblog :: shoulder pain from bench pressing

Anonymous said...

This is the right web site for everyone who wishes to find out about this topic.
You realize so much its almost hard to argue
with you (not that I really would want to…HaHa).
You definitely put a brand new spin on a subject that's been written about for a long time. Great stuff, just excellent!

Also visit my web site - neck And shoulder pain rego park

Anonymous said...

Today, I went to the beachfront with my children. I found
a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed
the shell to her ear and screamed. There was a hermit crab inside
and it pinched her ear. She never wants to go back! LoL I know this is entirely
off topic but I had to tell someone!

my weblog; breast enhancement liverpool

Anonymous said...

You ought to be a part of a contest for one of the highest quality websites on the
net. I'm going to highly recommend this web site!

Feel free to surf to my web blog - golf gloves

Anonymous said...

I was wondering if you ever considered changing the layout of your site?
Its very well written; I love what youve got to say. But
maybe you could a little more in the way
of content so people could connect with it better. Youve got an awful lot of text for only having
one or 2 pictures. Maybe you could space it out better?

Also visit my web blog - Outdoor Led Lighting

Anonymous said...

I read this piece of writing fully about the resemblance of hottest and previous
technologies, it's amazing article.

Here is my web blog - breast massage envy locations

Anonymous said...

Good way of explaining, and fastidious post to obtain data on the topic of my presentation focus, which i am
going to convey in college.

My web blog: back spasms treatment lower

Anonymous said...

Awesome website you have here but I was curious about if you knew of any
community forums that cover the same topics discussed here?
I'd really love to be a part of community where I can get responses from other experienced people that share the same interest. If you have any recommendations, please let me know. Bless you!

Here is my blog post las vegas golf instruction

Anonymous said...

Hello There. I found your blog using msn. This is a very well written article.
I will be sure to bookmark it and come back
to read more of your useful info. Thanks for the post.

I'll certainly comeback.

Also visit my site: www.youtube.com

Anonymous said...

It's the best time to make a few plans for the longer term and it's
time to be happy. I've read this publish and if I may just I desire to suggest you some fascinating issues or advice. Perhaps you could write subsequent articles relating to this article. I wish to read even more issues approximately it!

Also visit my homepage: impulse chiropractic adjustment tool

Anonymous said...

I believe everything published was very reasonable. But, think on
this, suppose you were to write a awesome post title?
I mean, I don't want to tell you how to run your blog, but suppose you added a post title to possibly grab a person's attention?
I mean "Create TextArea control with working Maxlength property" is a little boring.
You ought to look at Yahoo's front page and watch how they create post titles to grab viewers to open the links. You might add a video or a related pic or two to get people excited about what you've
written. In my opinion, it might make your
website a little bit more interesting.

My website - sushi zushi southlake hours

Anonymous said...

Peculiar article, just what I wanted to find.

my webpage - Outdoor Led Lighting

Anonymous said...

This is my first time pay a visit at here and i am in fact pleassant to read everthing at one place.


Feel free to visit my blog post; virility exercise

Anonymous said...

Nice post. I was checking continuously this blog and I am impressed!
Extremely useful info particularly the final phase :
) I deal with such information a lot. I was looking for this certain
info for a long time. Thanks and best of luck.


my homepage :: newnorthcenter.org

Anonymous said...

With havin so much written content do you ever run into
any issues of plagorism or copyright infringement? My site has a lot
of exclusive content I've either authored myself or outsourced but it appears a lot of it is popping it up all over the web without my agreement. Do you know any solutions to help stop content from being stolen? I'd definitely appreciate it.


Feel free to visit my website - cervical subluxation

Anonymous said...

If you want to grow your know-how only keep visiting this web
site and be updated with the newest news update posted here.


my weblog - Orlando Chiropractor

Anonymous said...

I really like your blog.. very nice colors & theme.
Did you design this website yourself or did you hire someone to
do it for you? Plz answer back as I'm looking to construct my own blog and would like to know where u got this from. thank you

Here is my weblog ... personal injury attorney

Anonymous said...

Thanks very interesting blog!

Take a look at my blog: physical science tillery 9th edition

Anonymous said...

Its like you read my mind! You seem to know so much about this,
like you wrote the book in it or something. I think that you
can do with some pics to drive the message home a bit, but instead of
that, this is wonderful blog. An excellent read. I will definitely be back.


Also visit my blog; myofascial pain syndrome

Anonymous said...

Pretty component to content. I just stumbled upon your site
and in accession capital to claim that I acquire in fact enjoyed account your blog posts.
Any way I'll be subscribing on your augment or even I achievement you get right of entry to consistently quickly.

my site; sushi bar green tea stash