问题描述
限时送ChatGPT账号..我有一个绑定到 ObjectDataSource 的 GridView.我也让它支持编辑,效果很好.但是,我想安全地对显示的 HtmlEncode 文本进行编码,因为我们确实允许在某些字段中使用特殊字符.这与标准 BoundFields 无关,因为我只是将 HtmlEncode 设置为 true.
I have a GridView bound to an ObjectDataSource. I've got it supporting editing as well, which works just fine. However, I'd like to safely HtmlEncode text that is displayed as we do allow special characters in certain fields. This is a cinch to do with standard BoundFields, as I just set HtmlEncode to true.
但是为了设置验证控件,需要改用 TemplateFields.如何轻松地添加 HtmlEncoding 以这种方式输出?这是一个 ASP.NET 2.0 项目,因此我使用了较新的数据绑定快捷方式(例如 Eval
和 Bind
).
But in order to setup validation controls, one needs to use TemplateFields instead. How do I easily add HtmlEncoding to output this way? This is an ASP.NET 2.0 project, so I'm using the newer data binding shortcuts (e.g. Eval
and Bind
).
我想做的事情如下:
<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBoxDescription" runat="server"
Text='<%# System.Web.HttpUtility.HtmlEncode(Bind("Description")) %>'
ValidationGroup="EditItemGrid"
MaxLength="30" />
<asp:Validator ... />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelDescription" runat="server"
Text='<%# System.Web.HttpUtility.HtmlEncode(Eval("Description")) %>' />
</ItemTemplate>
</asp:TemplateField>
但是,当我以这种方式尝试时,出现以下错误:
However, when I try it this way, I get the following error:
CS0103:名称绑定"不存在在当前上下文中
CS0103: The name 'Bind' does not exist in the current context
推荐答案
现在可以使用 ASP.NET 4 中引入的新 HTML 编码数据绑定语法来做到这一点.
This is now possible to do using the new HTML encoding databinding syntax introduced in ASP.NET 4.
您可以简单地使用:
<%#: Eval("MyField") %>
或
<%#: Bind("MyField") %>
注意井号/井号后面的冒号就这么简单.
这篇关于如何将 HtmlEncode 与 TemplateFields、数据绑定和 GridView 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论