Replace (Environment.NewLine, "<br/">") does not work properly when displaying text on a web page
I'm not sure why the following code does not work 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.
I intend to output the line to the web page and replace the line breaks with the br tag so that it can display on the page correctly. However, nothing is being replaced. When viewing the source of the page, it looks like the rows continue to exist in the row. Similarly, when displaying a line through MsgBox, it also displays normally. I also tried wrapping the output with a pre
tag, and it also correctly displays line breaks.
The string was originally entered by the user through the use of asp: Textbox and stored in the MSSQL database. It is retrieved and displayed on another web page using the asp: Literal control. The only thing that happens with the string before it is sent is that it is textbox.Text.Trim())
i.e. textbox.Text.Trim())
.
Perhaps there is something that I have not considered?
Edit # 1: Due to time constraints, I decided to just wrap the text with a pre
tag. This is an acceptable job as it keeps line breaks. Unfortunately, it does not explain why the code did not work in the first place. In the meantime, I will leave the question unanswered until I find the right answer.
Edit # 2: Solution found and answered below. The UpdatePanel tag has been added to the question for future reference.
After revising this issue, I found that the upgrade issues caused the problem.
After a few more tests, I saw that the line contains 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 a string before being sent to the database, as well as on a string retrieved from the database.
From my point of view, there should have been something in the markup that removed line breaks. At first, I thought the Literal, Textbox, and Label elements did something funny for line breaks when extracting code. This is not the case (and it would be strange if Microsoft allowed this).
A few more times with the help of the code I found that all these controls are inside the UpdatePanel. Recalling my previous negative impressions of UpdatePanels, I thought then that it might just be the culprit. I deleted it and now, line breaks did not disappear in the code. The code I wrote above returned true.
Both pages had controls for sending text and displaying text inside the UpdatePanel. I found that line break disappears when extracting text from the page. When displaying text on the page, UpdatePanel does not change the line in any way. The following code turned out to be very good:
litMessage.Text = messageBody.Replace(Environment.NewLine, "<br/>")
Although I still don't know why line breaks were correctly displayed when using the pre
tag or even displaying the resulting value using the javascript warning window.
If you use UPDATEPANEL, myText..Replace(Environment.NewLine, "<br/>")
DOES NOT WORK. I found the hard way (2 hours already)
The solution (when using updatepanel) is to install "PostBackTrigger" as follows:
<asp:UpdatePanel runat="server" ID="pnlUpdate" UpdateMode="Conditional"> <Triggers> <asp:PostBackTrigger ControlID="btnDoThatShitAlready" /> </Triggers>
I just found this, looking for a solution to replace text not working in a literal control when it is in form. The way I solved this was that in data binding for the literal, pass the text to a string, replace the string, then return the processed text to the text box.
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
Hope this helps you and everyone who can visit this site for a solution.
Regarding 4 tests above this 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 these tests and they didn't work either. Then I noticed that there is no test only on vbLf, so I tested it. BINGO! That's all!
So it works ...
Label.Text = MultiLineTextBox.Text.Replace(ControlChars.Lf, "<br />")
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 />");
Output signal
Text sample to replace carriage return + newline (<br />) and<br /> the Environment.NewLine and <br /> and the unix <br /> new line character.