Dude, where is my MaxLength for multiline TextBoxes?

Have you tried to add a MaxLength property to a multiline TextBox control? I was doing this recently, adding that property in the code behind, and when I viewed the page, the property was gone. Poof, no error, no MaxLength property, nothing. As I found out, that wonderful property is not valid on TextBox controls with a TextMode of “MultiLine”. What to do, what to do…

There are a couple of ways to handle this. I decided to just chop off the last few characters in the TextBoxI know, it’s probably offensive to users with delicate egos, so call me cruel. I first created a JavaScript function in the ASPX page that truncated the value in the TextBox:

    function truncMaxLength(txtControl,maxLength){  
        try{  
            if(txtControl.value.length > maxLength)
                txtControl.value = txtControl.value.substring(0, maxLength);
           }catch(e){ 
           }  
    }

And to call this from code behind, I simply added it to the Attributes collection of the TextBox, using the “onblur” event to call it whenever the TextBox loses focus:

    rdMultilineTxtBox.Attributes.Add("onblur", "truncMaxLength(this,100);");

 

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: