简体   繁体   English

ASP.NET HTML编码的数据绑定和br标签

[英]ASP.NET HTML Encoded Data-Bind and br tag

I'm trying to encode data that is to be displayed on a separate page (.aspx) after a user submits a form with their information and questions. 我正在尝试对要在用户提交其信息和问题的表单后显示在单独页面(.aspx)上的数据进行编码。 There are both text input and textarea fields that the user can fill out. 用户可以填写文本输入字段和文本区域字段。

I'm using the <%#: %> expression to place this data on the page since it does the HTML encoding, but have run into an issue with the encoding also encoding the br tags as text instead of actual line breaks. 我正在使用<%#:%>表达式将这些数据放置在页面上,因为它执行HTML编码,但是在编码方面也遇到了问题,也将br标签编码为文本而不是实际的换行符。

Is there a way to make it so that the br tags still cause a line break, but leave everything else encoded using this method? 有没有一种方法可以使br标签仍然引起换行,但使用此方法保留其他所有编码?

Example of user input: 用户输入的示例:

Hi

This is what should be responded with.

Here is an example of the code that places it on the page: 这是将其放置在页面上的代码示例:

<p><%#: ((DiscussionThread.Discussion)Container.DataItem).Text %></p>

What it currently displays on the page: 当前在页面上显示的内容:

Hi<br /><br />This is what should be responded with.

Thanks in advanced! 提前致谢!

Don't know, will it help you in your case or no, but I always use next way: 不知道,这是否对您有帮助,但我总是使用以下方式:

string forInput = yourTextBoxID.Text.ToString().Replace(Environment.NewLine, "%NL%");
string forOutput = forInput.Replace("%NL%", Environment.NewLine);

If in forOutput Environment.NewLine will not work, change it to <br /> 如果在forOutput Environment.NewLine中不起作用,请将其更改为<br />

%NL% it is just a identificator, u may change to anything else. %NL%它只是一个标识符,您可以更改其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM