哪个是打印纯文本的服务器端控件?
我知道(感谢我之前的一个问题)服务器端控件会自动对其内容进行编码,而 Response.Write(或简短版本<%=%>)写入原始输出。
那么,在 .NET (3.5+)
中,哪个服务器端控件可以写入编码/转义(“安全”)纯文本?
I know (thanks to a my previous SO question) that server-side controls automatically encode their content while Response.Write (or the short version <%=%>) writes raw output.
So, which is the server-side control, in .NET (3.5+)
, that write encoded/escaped ("safe") plain text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标签控件将对您的内容进行编码放入其中。因此,如果您将其
Text
属性设置为“hello my "nickname" is ",它将输出类似hello my "nickname" 的 HTML是
。如果您不希望 ASP.NET 对
Text
属性进行编码,请使用 文字控制。这将输出文本,就像您输入的一样。这样,您可以将其Text
属性设置为something
。您将在 HTML 中得到准确的结果。您的用户将看到某些内容(因此,以粗体显示)。所以,我认为您需要 Label 控件。
更新:
《使用 Microsoft .NET Framework 4 进行 Web 应用程序开发》一书(考试 70-515 的培训工具包)让我感到困惑。它说:
所以,我相信两者都可以使用。标签只会将 html 添加到您输入的文本中(即 span 标签)。 Literal 不会,但是,就像 bukko 所说,您应该正确设置 Mode 属性。
The Label control will encode what you put into it. So if you set its
Text
property to "hello my "nickname" is ", it will output HTML likehello my "nickname" is <markzzz>
.If you don't want ASP.NET to encode the
Text
property, use the Literal control. This will output the text just like you entered it. This way, you could set itsText
property to<strong>something</strong>
for example. And you would get exactly that in the HTML. Your user would see something (so, in bold).So, I think you want the Label control.
Update:
The book 'Web Applications Development with Microsoft .NET Framework 4' (the training kit for exam 70-515) is what got me confused. It says:
So, I believe both can be used. The Label will just add html to the text you entered (namely, a span tag). The Literal won't, but, like bukko said, you should set the Mode property correctly.
我对这个问题有点困惑,但如果您想知道哪些 ASP 控件将使用自动 HTML 编码显示文本,我确信
asp:textbox
和asp:textarea< /code> 两者都可以。
但是,您也可以使用 Response.Write 来实现此目的,只要您先对其进行编码即可,例如请
参阅 http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.htmlencode.aspx 了解更多信息。
或者,通过 .NET 4,您可以使用旧的经典 ASP 块进行编码输出:
这是否正是您想要的?
I'm a bit confused by the question, but if you want to know which ASP controls will display text with automatic HTML encoding, I'm sure that
asp:textbox
andasp:textarea
both do.However, you can also achieve this with
Response.Write
as long as you encode it first, e.g.See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.htmlencode.aspx for more info.
Or, with .NET 4, you can use the old classic ASP nuggets for encoded output:
Is this more what you wanted?