哪个是打印纯文本的服务器端控件?

发布于 2025-01-06 00:55:17 字数 271 浏览 1 评论 0原文

我知道(感谢我之前的一个问题)服务器端控件会自动对其内容进行编码,而 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2025-01-13 00:55:17

标签控件将对您的内容进行编码放入其中。因此,如果您将其 Text 属性设置为“hello my "nickname" is ",它将输出类似 hello my "nickname" 的 HTML是

如果您不希望 ASP.NET 对 Text 属性进行编码,请使用 文字控制。这将输出文本,就像您输入的一样。这样,您可以将其 Text 属性设置为 something。您将在 HTML 中得到准确的结果。您的用户将看到某些内容(因此,以粗体显示)。

所以,我认为您需要 Label 控件。

更新:

《使用 Microsoft .NET Framework 4 进行 Web 应用程序开发》一书(考试 70-515 的培训工具包)让我感到困惑。它说:

当您想要将原始 HTML 添加到页面时,请使用 Literal 控件,
不需要 ASP.NET 提供任何额外的处理。在
乍一看,Literal 控件与 Label 非常相似
控制。然而,Literal 并不继承自 WebControl,正如 Label 一样
做。此外,Literal 控件不添加任何 HTML 元素
到网页,而 Label 则呈现为标签。这
意味着 Literal 控件没有 style 属性,并且您
因此无法将样式应用于其内容。

文字控制是
当您需要动态添加文本到页面的输出时很有用
(来自服务器)但不想使用标签。如果您的文字是
静态的,您可以简单地将其添加到页面的标记中(您不需要
需要标签或文字控件)。 Literal 控件包含
Mode 属性,用于指定对
Text 属性的内容。可用的模式及其
说明如表4-2所示。

所以,我相信两者都可以使用。标签只会将 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 like hello 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 its Text 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:

Use the Literal control when you want to add raw HTML to a page,
without requiring ASP.NET to provide any additional processing. At
first glance, the Literal control seems very similar to the Label
control. However, Literal does not inherit from WebControl, as Label
does. Additionally, the Literal control does not add any HTML elements
to the webpage, whereas the Label is rendered as a tag. This
means that the Literal control does not have a style property, and you
therefore cannot apply styles to its content.

The Literal control is
useful when you need to add text to the output of the page dynamically
(from the server) but do not want to use a Label. If your text is
static, you can simply add it to the markup of the page (you do not
need a Label or a Literal control). The Literal control contains the
Mode property, which is used to specify any particular handling of the
content of the Text property. The modes available and their
descriptions are shown in Table 4-2.

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.

背叛残局 2025-01-13 00:55:17

我对这个问题有点困惑,但如果您想知道哪些 ASP 控件将使用自动 HTML 编码显示文本,我确信 asp:textboxasp:textarea< /code> 两者都可以。

但是,您也可以使用 Response.Write 来实现此目的,只要您先对其进行编码即可,例如请

Response.Write( Server.HTMLEncode("The <select /> control!") );

参阅 http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.htmlencode.aspx 了解更多信息。

或者,通过 .NET 4,您可以使用旧的经典 ASP 块进行编码输出:

<% string output_text = "<select />"; %>
<%= output_text %>

这是否正是您想要的?

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 and asp:textarea both do.

However, you can also achieve this with Response.Write as long as you encode it first, e.g.

Response.Write( Server.HTMLEncode("The <select /> control!") );

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:

<% string output_text = "<select />"; %>
<%= output_text %>

Is this more what you wanted?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文