Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace(Environment.NewLine, "<br/>") is not working as expected when displaying text on a web page

I'm not sure why the following code is not working as intended:

litMessage.Text = messageBody.Replace(Environment.NewLine, "<br/>")

Where litMessage is the name of the literal control and messageBody is the name of the string variable.

My intention is to output a string onto a web page and to replace the line breaks with a br tag so that it can be displayed on the page correctly. However, nothing is replaced. When viewing the page source, it looks like the line breaks still exist in the string. Similarly, when displaying the string via MsgBox, it displays normally as well. I have also tried wrapping the output with the pre tag and it displays the line breaks properly as well.

The string was originally entered by a user through the use of an asp:Textbox and saved into a MSSQL database. It's retrieved and displayed on another webpage using an asp:Literal control. The only thing that happens to the string before it is submitted is that it is trimmed (i.e. textbox.Text.Trim()).

Perhaps there is something I did not consider?

Edit #1: Due to time constraints, I've decided to just wrap the text with the pre tag. It's an acceptable work around as it preserves the line breaks. Unfortunately, it doesn't explain why the code didn't work in the first place. In the meantime, I'll just leave the question unanswered until I find a proper answer.

Edit #2: Solution found and answered below. UpdatePanel tag added to the question for future reference.

like image 372
nullexception Avatar asked Nov 30 '22 07:11

nullexception


2 Answers

After revisiting this issue, I have found that updatepanels were causing the problem.

Doing some more testing, I looked into what the string contained using the following code:

MsgBox(textbox.Text.Contains(vbCr))
MsgBox(textbox.Text.Contains(vbCrLf))
MsgBox(textbox.Text.Contains(vbNewLine))
MsgBox(textbox.Text.Contains(Environment.Newline))

All four statements returned false. This was tested on the string before sending to the database as well as on the string retrieved from the database.

From my point of view, there had to be something on the markup that was removing the line breaks. At first, I thought the Literal, Textbox, and Label controls did something funny to line breaks when retrieved in the code behind. This is not the case (and would be bizarre if Microsoft let this happen).

Combing through the code some more, I found that all these controls were inside an UpdatePanel. Remembering my previous negative experiences with UpdatePanels, I then thought that this might just be the culprit. I removed it and lo and behold, the line breaks did not go away in the code. The code I posted just above all returned true.

Both pages had the controls for submitting the text and displaying the text inside an UpdatePanel. I found that the line breaks disappeared when retrieving the text from the page. When displaying the text onto the page, the UpdatePanel does not alter the string in any way. The following code ended up working just fine:

litMessage.Text = messageBody.Replace(Environment.NewLine, "<br/>")

Although I'm still not sure why line breaks were still displayed correctly when using the pre tag or even displaying the retrieved value using a javascript alert box.

like image 95
nullexception Avatar answered Dec 02 '22 22:12

nullexception


It's hard to say from the code you've posted, but if you're passing the myString varaible itself to the page (and not the return value of the Replace function) you'll need to do -

myString = myString.Replace(Environment.NewLine, "<br/>")

before passing it to the page.

like image 42
ipr101 Avatar answered Dec 02 '22 22:12

ipr101