nullexception
nullexception

Reputation: 131

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.

Upvotes: 4

Views: 24866

Answers (12)

Ali Mahmoodi
Ali Mahmoodi

Reputation: 1194

I'm using this code and it works : Instead of

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

use

.Replace("\n", "<br />")

Hope to be useful.

Upvotes: 0

Gunjesh Kumar
Gunjesh Kumar

Reputation: 41

I wasted my two hours finding what was actually wrong. Well, solution is instead of Environment.NewLine, use ControlChars.Lf

You can remove updatepanel which will sort out the problem for future data. But if you already have some data, using ControlChars.Lf will help.

Upvotes: 2

Hal Driver
Hal Driver

Reputation: 21

Regarding the 4 tests above that failed (author: nullexception)...

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

I tried those tests and they failed for me as well. Then, I noticed there was no test for just vbLf, so I tested it. BINGO! That was it!

So, this works...

Label.Text = MultiLineTextBox.Text.Replace(ControlChars.Lf, "<br />")

Upvotes: 2

blu3drag0n
blu3drag0n

Reputation: 71

What you are really looking for is:

@Html.Raw(@Regex.Replace(input_to_be_printed_correctly, @"\r\n?|\n", "<br />"))

Upvotes: 0

Raju
Raju

Reputation: 151

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

It has worked for me. Hope this might work for you as well.

Upvotes: 1

Nudier Mena
Nudier Mena

Reputation: 3274

You can use it like this, because the System.NewLine returns a "\r\n" string dim messageBody as string = "some text here";

litMessage.Text = messageBody.Replace("\r\n", "< br />");

Upvotes: 1

Dayakar
Dayakar

Reputation: 1

To put the label text next line we can use the below mentioned solution.

<asp:Label ID="lblAppointmentLocation" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Location").ToString().Replace(Environment.NewLine, "<br/>").Replace("\n", "<br />") %>'></asp:Label>

Upvotes: 0

Robert
Robert

Reputation: 31

If you use an UPDATEPANEL the myText..Replace(Environment.NewLine, "<br/>") WILL NOT WORK. I found out the hard way (spent 2 hours already)
The solution (when using updatepanel) is to set a "PostBackTrigger" like this:

<asp:UpdatePanel runat="server" ID="pnlUpdate" UpdateMode="Conditional">

<Triggers>
<asp:PostBackTrigger ControlID="btnDoThatShitAlready" />
</Triggers>

Upvotes: 3

Atrius
Atrius

Reputation: 21

Just found this while looking for a solution for replace not working with text in the literal control while it resides within a formview. The way I solved it was, in the databinding for the literal, to pass the text to a string, do the replace on the string then put the processed text back into the literal's text field.

Protected Sub MyLit_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    Dim text1 As String
    text1 = CType(myFormView.FindControl("MyLit"), Literal).Text
    text1 = Replace(text1, Environment.NewLine, "<br />")
    CType(myFormView.FindControl("MyLit"), Literal).Text = text1
End Sub

I hope this helps you, and anyone else who might visit this site for a solution.

Upvotes: 2

nullexception
nullexception

Reputation: 131

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.

Upvotes: 6

Adrian Iftode
Adrian Iftode

Reputation: 15663

var str = "Text sample to replace carriage return + newline ("
+ (char)13 + (char)10 
+ @") and
the Environment.NewLine and " 
+ Environment.NewLine
+ " and the unix \n new line character.";

str.Replace("\r\n","<br />")
       .Replace(Environment.NewLine, "<br />")
       .Replace("\n", "<br />");

The output is

Text sample to replace carriage return + newline (<br />) and<br />  the Environment.NewLine and <br /> and the unix <br /> new line character.

Upvotes: 0

ipr101
ipr101

Reputation: 24236

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.

Upvotes: 3

Related Questions