Hi,
I need a control that can satisfy the following:
1. Must have word wrap (like a multiline textbox or textarea). Word wrap should support pasted text as well as entered text.
2. Must be able to specify maximum line length (35 characters) and maximum number of lines (4).
3. When posted back, I need access to each line of text as it looked on the clients page, so it can be stored in 4 different database fields that each have a maximum length of 35 characters.
This is all in order to spare users the hassle of entering their four permitted lines of text in 4 separate text boxes.
Do you have tips for getting this kind of functionality? I am hoping I can avoid making this control from scratch. I have been looking at commercial products but they seem to center on HTML editing functionality. Also, I am not convinced that I will get 2. and 3.
Thank you.
![]() |
-1 |
![]() |
Hi, i think this can be achieved with the help of javascript. Like this:
<asp:TextBox ID="TextBox1" runat="server" Height="124px" TextMode="MultiLine" Width="267px" onkeydown="isValidInput(event);"></asp:TextBox>
function isValidInput(e)
{
var obj = document.getElementById("TextBox1");
var v = obj.value;
var array = v.split("\n");
if(e.keyCode == 13 && array.length > 3)
return false;
}
More info about it can be found here:
http://www.w3schools.com/js/default.asp
Hope it helps.
![]() |
-1 |
![]() |
No. A multiline textbox or textarea does not insert new line characters when it performs word wrap.
This javascript function can only be used to limit the number of line breaks the user inserts manually. So it does not satisfy 3.
Thanks anyway.
![]() |
-1 |
![]() |
What exactly is wrong with using 4 separate textboxes? If the text really needs to be divided up this way, it would seem to make sense.
Otherwise, why not just use an onkeydown event handler to check each line? You could do a split on the newline character, parse each array item (i.e. line) to see if it was longer than 35, and if so, add a carriage return. If the total number of lines exceeds 4, you'd have to decide how you want to handle that for the user.
This way when you post back, the lines are still correct because you've added the formatting to the textbox contents, and if the user needs to pull it up again, you just put each line into the box with CRs between them.Of course that seems like a lot of trouble just to avoid using 4 textboxes (and if the user turns Javascript off or has accessibility requirements, you could be hosed).
Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
![]() |
-1 |
![]() |
What is wrong abour 4 textboxes is that some users have been used to typing mindlessly and now thay have to think about where to divide their text and change focus manually.
It doesn't matter that the control is scripted. I just feel that there must have been done something like this before to save me from coding everything from scratch.
I thought about doing it the way you propose... but I would lose the word wrap functionality or have to implement it myself - none of that is acceptable right now.
![]() |
1 |
![]() |
In the end I found out how to do it, and without javascript even.
The control I needed was the normal html textarea control.
The trick is that the html TextArea has an attribute value wrap='hard', which is not mirrored in the ASP.NET multiline textfield. Setting this attribute will cause ordinary line breaks ('\r\n') to be inserted wherever word wrap has occurred. when the page is posted back. Then I can split the textarea content into lines on the server.
<
textarea id="taMessage" Mandatory="false" wrap="hard" cols="36" rows="4" runat="server" style="font-family:Lucida Console, Monospace; font-size: 13px; overflow-y:hidden"></textarea>I use a monospace font and the cols attribute to set the maximum line length - cols for some reason needs to be set 1 higher than the number of characters I want to allow.
The main drawback is that there is no way to stop the user from entering more than 4 lines. The content wil scroll to accommodate the extra lines. Javascript cannot be used to count the number of lines because it is impossible to detect the soft line breaks that result from word wrap. So there is no way around validating the number of lines on the server and clipping the content and/or warning the user.
![]() |
1 |
![]() |
Well, duh; I should kick myself for not thinking to mention wrap="hard".
Thanks for keeping us updated; be sure to write back if you find a way around it (maybe you could post to a page that doesn't return any data and then get a quick line check via AJAX?)
Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
![]() |
-1 |
![]() |