Problem : Maxlength property is ignored in multiline textboxes. 
Details of problem : When the TextMode property of a textbox is set to Multiline the MaxLength property will not limit the maximum length of user input. This is due to the fact that multiline textboxes are rendered to the browser actually as a textarea which has no maxlenth property. (I believe a visual cue, like disable maxlength, when multiline is selected would be an added value.)
Steps to reproduce this issue (for those who don’t know that its a problem)
- Add a Multiline textbox (txtMl.TextMode = Multiline)
-
Add another singleline textbox (txtSl.TextMode = SingleLine).
-
Set the MaxLength of Both to 10.
-
Run the project
- Observe: You can type as many characters as you wants in the multiline Textbox but not in the singleline Textbox
Solution ~
This problem can be solved by using Javascript to track how many characters typed in textbox. If the length of characters are over max number then we can simply ignore what the user type by returning “false” in javascript function.
Step #1. Put the following function in your page.
<script language="javascript">
function textboxMultilineMaxNumber(txt,maxLen){
try{
if(txt.value.length > (maxLen-1))
return false;
}catch(e){
}
}
</script>
<script language="javascript">
function textboxMultilineMaxNumber(txt,maxLen){
try{
if(txt.value.length > (maxLen-1))return false;
}catch(e){
}
}
</script>
Step #2. Put the JS function in onkeypress event of Textbox.
<asp:textbox id="txtMl" runat="server" Width="232px" TextMode="MultiLine"
onkeypress=<strong>"return textboxMultilineMaxNumber(this,10)"</strong>></asp:textbox>
As a side note remember to validate your data server side as javascript can be and often is disabled ...
Run the sample
Observe: Both textboxes restrict the characters you type to 10 as desired.
The main idea for this tip comes from Mike Sync's site. I just wanted to make a non download type of tip out of it.
HTH,