Html Replace(Environment.NewLine, "<br/>") 在网页上显示文本时无法按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8262489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:39:53  来源:igfitidea点击:

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

asp.nethtmlvb.netupdatepanelline-breaks

提问by nullexception

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

我不确定为什么以下代码无法按预期工作:

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

Where litMessageis the name of the literal control and messageBodyis the name of the string variable.

其中litMessage是文字控件messageBody的名称, 是字符串变量的名称。

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 pretag and it displays the line breaks properly as well.

我的目的是将一个字符串输出到网页上,并用 br 标签替换换行符,以便它可以正确显示在页面上。然而,什么都没有被取代。查看页面源代码时,看起来字符串中仍然存在换行符。同样,当通过 MsgBox 显示字符串时,它也可以正常显示。我还尝试用pre标签包装输出,它也正确显示换行符。

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()).

该字符串最初由用户通过使用 asp:Textbox 输入并保存到 MSSQL 数据库中。它使用 asp:Literal 控件检索并显示在另一个网页上。字符串在提交之前发生的唯一事情是它被修剪(即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 pretag. 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.

编辑 #1:由于时间限制,我决定只用pre标签包裹文本。这是一个可以接受的解决方法,因为它保留了换行符。不幸的是,它并没有解释为什么代码首先不起作用。同时,在找到正确答案之前,我将不回答这个问题。

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

编辑 #2:在下面找到并回答了解决方案。UpdatePanel 标记添加到问题以供将来参考。

回答by nullexception

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).

从我的角度来看,标记上必须有一些东西可以删除换行符。起初,我认为 Literal、Textbox 和 Label 控件在后面的代码中检索时对换行符做了一些有趣的事情。事实并非如此(如果微软让这种情况发生,那就太奇怪了)。

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.

进一步梳理代码,我发现所有这些控件都在一个 UpdatePanel 中。想起我之前使用 UpdatePanels 的负面经历,我认为这可能就是罪魁祸首。我删除了它,瞧,代码中的换行符并没有消失。我在上面发布的代码全部返回 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:

两个页面都有用于提交文本和在 UpdatePanel 中显示文本的控件。我发现从页面检索文本时换行符消失了。在页面上显示文本时,UpdatePanel 不会以任何方式更改字符串。以下代码最终工作正常:

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

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

虽然我仍然不确定为什么在使用pre标签时仍然正确显示换行符,甚至使用 javascript 警报框显示检索到的值。

回答by ipr101

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

从您发布的代码很难说,但是如果您将myString变量本身传递给页面(而不是Replace函数的返回值),您需要执行以下操作 -

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

before passing it to the page.

在将其传递到页面之前。

回答by Robert

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:

如果您使用 UPDATEPANEL,则将不起作用myText..Replace(Environment.NewLine, "<br/>")。我发现了困难的方法(已经花了 2 个小时)
解决方案(使用 updatepanel 时)是像这样设置“PostBackTrigger”:

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

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

回答by Atrius

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.

我希望这对您以及可能访问此站点以寻求解决方案的任何其他人有所帮助。

回答by Hal Driver

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

关于上面失败的 4 个测试(作者: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!

我尝试了这些测试,但它们对我来说也失败了。然后,我注意到没有针对 vbLf 的测试,所以我测试了它。答对了!就是这样!

So, this works...

所以,这有效......

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

回答by Gunjesh Kumar

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

我浪费了两个小时来寻找真正的错误。那么,解决方案是代替 Environment.NewLine,使用 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.

您可以删除更新面板,它将为未来的数据整理出问题。但是如果您已经有一些数据,使用 ControlChars.Lf 会有所帮助。

回答by Nudier Mena

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

您可以像这样使用它,因为它System.NewLine返回一个"\r\n"字符串 dim messageBody as string = "some text here";

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

回答by Raju

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

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

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

它对我有用。希望这也适用于您。

回答by Enter The Void

What you are really looking for is:

你真正要找的是:

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

回答by Dayakar

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>