我不确定为什么以下代码无法按预期工作:
litMessage.Text = messageBody.Replace(Environment.NewLine, "<br/>")
其中 litMessage
是文字控件的名称,messageBody
是字符串变量的名称。
我的目的是输出一个字符串到网页上,并用br标签替换换行符,这样就可以正确显示在页面上。然而,没有任何东西被取代。查看页面源代码时,看起来字符串中仍然存在换行符。同样,通过MsgBox显示字符串时,也能正常显示。我还尝试使用 pre
标签包装输出,它也可以正确显示换行符。
该字符串最初由用户通过使用 asp:Textbox 输入并保存到 MSSQL 数据库中。使用 asp:Literal 控件检索它并显示在另一个网页上。字符串在提交之前唯一发生的事情是它被修剪(即 textbox.Text.Trim())
。
也许有什么我没有考虑到的?
编辑#1:由于时间限制,我决定仅使用 pre
标记将文本换行。这是一种可以接受的解决方法,因为它保留了换行符。不幸的是,它并没有解释为什么代码一开始就不起作用。与此同时,我将不回答这个问题,直到找到正确的答案。
编辑#2:在下面找到并回答了解决方案。 UpdatePanel 标签已添加到问题中以供将来参考。
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.
发布评论
评论(12)
重新审视这个问题后,我发现 updatepanels 导致了这个问题。
做了更多测试,我使用以下代码查看了字符串包含的内容:
所有四个语句都返回 false。在发送到数据库之前以及从数据库检索的字符串上对此进行了测试。
从我的角度来看,标记上必须有一些东西可以删除换行符。起初,我认为 Literal、Textbox 和 Label 控件在后面的代码中检索时对换行符做了一些有趣的事情。事实并非如此(如果微软允许这种情况发生,那就太奇怪了)。
进一步梳理代码,我发现所有这些控件都位于 UpdatePanel 内。回想起我之前使用 UpdatePanels 的负面经历,我想这可能就是罪魁祸首。我删除了它,你瞧,代码中的换行符并没有消失。我上面发布的代码全部返回 true。
两个页面都有用于提交文本和在 UpdatePanel 内显示文本的控件。我发现从页面检索文本时换行符消失了。当在页面上显示文本时,UpdatePanel 不会以任何方式更改字符串。下面的代码最终工作得很好:
虽然我仍然不确定为什么在使用
pre
标记甚至使用 javascript 警报框显示检索到的值时仍然正确显示换行符。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:
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:
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.从您发布的代码中很难看出,但如果您将
myString
变量本身传递给页面(而不是Replace
函数的返回值) 您需要执行 - 操作。在将其传递到页面之前,
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 theReplace
function) you'll need to do -before passing it to the page.
如果您使用 UPDATEPANEL,则
myText..Replace(Environment.NewLine, "
将不起作用。我很难找到方法(已经花了 2 个小时)")
解决方案(使用 updatepanel 时)是设置一个“PostBackTrigger”,如下所示:
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:
刚刚在寻找解决方案时发现了这一点,以替换位于表单视图中的文字控件中的文本。我解决这个问题的方法是,在文字的数据绑定中,将文本传递给字符串,对字符串进行替换,然后将处理后的文本放回到文字的文本字段中。
我希望这对您以及可能访问此网站寻求解决方案的任何其他人有所帮助。
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.
I hope this helps you, and anyone else who might visit this site for a solution.
关于上面失败的 4 个测试(作者:nullException)...
我尝试了这些测试,但它们对我来说也失败了。然后,我注意到没有针对 vbLf 的测试,所以我测试了它。宾果!就是这样!
所以,这有效...
Regarding the 4 tests above that failed (author: nullexception)...
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...
我浪费了两个小时才发现到底出了什么问题。
好吧,解决方案是使用 ControlChars.Lf 代替 Environment.NewLine
您可以删除 updatepanel,这将为将来的数据解决问题。但如果您已经有一些数据,使用 ControlChars.Lf 会有所帮助。
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.
您可以像这样使用它,因为
System.NewLine
返回一个"\r\n"
字符串暗淡的 messageBody as string = "这里有一些文字";
You can use it like this, because the
System.NewLine
returns a"\r\n"
stringdim messageBody as string = "some text here";
litMessage.Text = messageBody.Replace("\\\\n","
").Replace(Environment.NewLine, "
")
它有为我工作。希望这也对你有用。
litMessage.Text = messageBody.Replace("\\\\n","< br />").Replace(Environment.NewLine, "< br />")
It has worked for me. Hope this might work for you as well.
输出是
The output is
要将标签文本放在下一行,我们可以使用下面提到的解决方案。
To put the label text next line we can use the below mentioned solution.
您真正寻找的是:
What you are really looking for is:
我正在使用这段代码并且它有效:
而不是
使用
希望有用。
I'm using this code and it works :
Instead of
use
Hope to be useful.