Weird TextBox behaviour

F

fd123456

The TextBoxes in ASP have a weird behaviour when it comes to handling
carriage returns. You can try this :

Create a Webform. On it, place a textbox, set it's TextMode property
to MultiLine.
Place a button. Double-click on the button and in it's event handler,
just type :

TextBox1.Text = TextBox1.Text ;

(if you're doing this in VB, just omit the ";").

Show that page in your favorite browser, or even inside the IDE. Type
two carriage returns (nothing fancy, plain old "Return" key), then any
text.

Now click on the button : the first CR disappears. Click again : the
next CR disappears. The rule is : if there is a CR at the top of the
text, it will be deleted.

Took me a while to figure, bacause at first I thought it was my
DataAdapter messing with the xml. I've searched the groups to no
avail. I'm doing this with VS 2003 and 2005 is not an option (yet).

Has anyone found a workaround to this? I'm stumped. Thanks in advance.

Michel
 
K

Kevin Spencer

I believe your problem may result from setting the textbox's text property
to its own text property, combined with a bit of weirdness on the part of
the .Net platform.

In HTML, there are 2 types of "TextBoxes." The single-line text box renders
(approximately) the following HTML:

<input type="text" name="something" id="something" value="something">

Note that the "value" attribute is an attribute. A "multi-line textbox" is a
different HTML element:

<textarea name="something" id="something" rows="3" columns="60">Content goes
here.
Content may contain carriage returns,
as demonstrated here. Note that content is unformatted text.</textarea>

Now, as you should know, HTML considers all "white space" characters,
including line feeds, tabs, spaces, etc. to be single spaces when rendered.
HTML allows elements to span more than one line. So, what happens when you
do this with a textarea?

<textarea name="something" id="something" rows="3" columns="60">
</textarea>

It would seem that there is nothing in the textarea. However, there IS
something: a carriage return. HTML rules allow you to break tags across
lines, and the above example is a perfectly legitimate textarea. But, due to
the pre-formatted nature of the text inside, it does contain a line feed.
Many developers often make the mistake of designing a textarea in this way,
and are puzzled by the extra line feed in their result. Now, I'm going to
theorize a bit here, and am guessing that the developers of ASP.Net saw this
trend, and decided to "correct" it, by eliminating any line feed at the
beginning of a textarea. My guess would be that if you don't reassign the
value, the PostBack will retain the value as original, as long as you've
enabled ViewState for it. Have you tried this?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top