如何使用HtmlEn code。与模板列,数据绑定,和一个GridView [英] How to use HtmlEncode with TemplateFields, Data Binding, and a GridView

查看:137
本文介绍了如何使用HtmlEn code。与模板列,数据绑定,和一个GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定到一个ObjectDataSource的GridView。我知道了配套的编辑为好,这工作就好了。不过,我想安全地HtmlEn因为我们允许在某些领域的特殊字符显示code文本。这是一个容易的事做标准绑定列,因为我只设置HtmlEn code为true。

但为了设置验证控件,需要使用模板列来代替。我如何轻松地添加HtmlEncoding输出这种方式?这是一个ASP.NET 2.0项目,所以我使用的是较新的数据绑定的快捷方式(如评估绑定 )。

我想要做的是类似如下:

 < ASP:的TemplateField的HeaderText =说明>
    <&EditItemTemplate的GT;
        < ASP:文本框ID =TextBoxDescription=服务器
                     文字='<%#System.Web.HttpUtility.HtmlEn code(绑定(说明))%GT;'
                     的ValidationGroup =EditItemGrid
                     MAXLENGTH =30/>
        < ASP:验证... />
    < / EditItemTemplate中>
    <&ItemTemplate中GT;
        < ASP:标签ID =LabelDescription=服务器
                   文字='<%#System.Web.HttpUtility.HtmlEn code(EVAL(说明))%GT;' />
    < / ItemTemplate中>
< / ASP:的TemplateField>

然而,当我尝试这种方式,我收到以下错误:


  

CS0103:'绑定'的名称不存在
  在目前情况下



解决方案

这是现在可以使用ASP.NET 4引入了新的HTML编码的数据绑定语法做

您可以简单地使用:

 <%#为Eval(MyField的)%>

或者

 <%#:绑定(MyField的)%>

注意英镑/井号冒号后就这么简单。

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.

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).

What I'd like to do is something like the following:

<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: The name 'Bind' does not exist in the current context

解决方案

This is now possible to do using the new HTML encoding databinding syntax introduced in ASP.NET 4.

You can simply use:

<%#: Eval("MyField") %>

Or

<%#: Bind("MyField") %>

Note the colon after the pound/hash sign It's as simple as that.

这篇关于如何使用HtmlEn code。与模板列,数据绑定,和一个GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆