prevent HTML编码中自动生成的GridView列 [英] Prevent HTML encoding in auto-generated GridView columns

查看:117
本文介绍了prevent HTML编码中自动生成的GridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定到一个DataTable,我建立一个GridView。表中的大多数列包含一个hypelinklink原始HTML,我想这HTML呈现在浏览器中的链接,但在GridView自动编码的HTML,所以它呈现为标记。

I have a GridView bound to a DataTable that I construct. Most columns in the table contain the raw HTML for a hypelinklink, and I would like that HTML to render as a link in the browser, but the GridView is automatically encoding the HTML, so it renders as markup.

我怎样才能避免这种没有明确添加超链接,或任何其他列?

How can I avoid this without explicitly adding HyperLink, or any other, columns?

推荐答案

只需设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.htmlen$c$c.aspx\"><$c$c>BoundColumn.HtmlEn$c$c属性设置为false:

&LT; ASP:BoundField的数据字段=HtmlLinkHtmlEn code =FALSE/&GT;

恐怕没有简单的方法来禁用一个 GridView控件<$c$c>AutoGenerateColumns<$c$c>=真正。不过,我可以认为这可能解决你所面临的问题,有两种解决方法:

I am afraid that there is no easy way to disable HTML encoding of the contents in a GridView with AutoGenerateColumns= true. However, I can think of two workarounds that might solve the problem you are facing:

选项1:继承了 GridView控件类,重写渲染法,循环通过所有细胞,德code的内容,执行基本方法之前:

Option 1: Inherit the GridView class, override the Render method, loop through all cells, decode their contents, before executing the base method:

for (int i = 0; i < Rows.Count; i++) 
{
    for (int j = 0; j < Rows[i].Cells.Count; j++) 
    {
        string encoded = Rows[i].Cells[j].Text;
        Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);
    }
}

选项2:在一个类从 GridView控件继承控制使用它,使自己的 DataTable的检查并创建一个明确的绑定列为每列:

Option 2: In a class inheriting from GridView or in the Page or Control using it, make your own inspection of the DataTable and create an explicit BoundColumn for each column:

foreach (DataColumn column in dataTable.Columns)
{
    GridViewColumn boundColumn = new BoundColumn
        {
            DataSource = column.ColumnName,
            HeaderText = column.ColumnName,
            HtmlEncode = false
        };
    gridView.Columns.Add(boundColumn);
}

这篇关于prevent HTML编码中自动生成的GridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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