views:

32

answers:

2

On aspx page I have asp:textbox multilined, where I type data. When I assign TextBox1.text to a string i get something like this:

"line1\r\nline2\r\nline3"

But if I try to set it back to literal, I get:

line1 line2 line3

(without the line barkes)

How to solve it? Is the only way to replace the \r\n with <br />?

A note: If I store the in put in database (sqlserver), and open the table in sql management studio -> edit rows-> I see string line1line2line3 (all together), but if try to copy to clipboard this content (Ctrl+C) only line1 is copied! Why?
Also, If i bind this data to a repeater, and Eval("myfield") to repeater I get the line brake!

+1  A: 

In C#

string htmlFormattetText = textStringFromDB.Replace("\r\n","<br />")

or in SQL

SELECT REPLACE(TextCol,'\r\n','<br />' FROM MyTable
Arkain
but why if I save input in database and then retrieve it , it works without any replacing?
shlomjmi
Can you post the code that seems to work? And in that case I don't understand the problem. I thought you couldn't get the line breaks when you outputtet the \r\n in html, which is by design, html only looks for <br /> for line breaks. Can you clarify?
Arkain
I mean If I insert the text box content into database and then load it into a div I get the text with line brakes
shlomjmi
Can you post the code that you use to load in the text into the DIV tag?
Arkain
A: 

Here's the trick. Your input is like this:

I have asp:textbox multilined

But your output is like this:

I try to set it back to literal

A literal control just renders raw html, and raw html ignores line breaks. The multi-line textbox renders as an html textarea element, and the value of a textarea respects line breaks.

Joel Coehoorn