0

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!

2 Answers 2

1

In C#

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

or in SQL

SELECT REPLACE(TextCol,'\r\n','<br />' FROM MyTable
4
  • but why if I save input in database and then retrieve it , it works without any replacing?
    – shlomjmi
    Jul 6, 2010 at 16:44
  • 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? Jul 6, 2010 at 16:48
  • 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
    Jul 6, 2010 at 16:52
  • Can you post the code that you use to load in the text into the DIV tag? Jul 6, 2010 at 18:30
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.