在DataTable ASP.NET中编写HTML [英] Write html in DataTable ASP.NET

查看:116
本文介绍了在DataTable ASP.NET中编写HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建一个DataTable,并且希望某些单元格的内容为html.

我认为以下内容说明了我想做的事...

I am creating a DataTable dynamically and would like the contents of some cells to be html.

I think the below explains what I would like to do...

DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("DummyColumnName"));
        dt.Rows.Add(12);
        dt.Rows.Add(13);
        dt.Rows.Add(11);
        dt.Rows.Add("<div>dummy text</div>");
        myGridView.DataSource = dt;
        myGridView.DataBind();





html页面将显示html标签字符串< div>虚拟文本</div>而不是html本身





The html page will display the html tag string <div>dummy text</div> in the cell rather than the html itself

推荐答案

那么,您在问什么呢?如何将HTML文本放入您的数据表中?如果您已将HTML放在数据表中,是否无法在Web控件上呈现它?
So, what are you asking? How to get the HTML text into your datatable? If you have placed HTML in your datatable, are you having trouble rendering it on a web control?


Yes thats it, my current html (looking only at the specific cell) is written as this...


<td>&lt;div&gt;dummy text&lt;/div&gt;</td>


And what I would want is

<td><div>dummy text</div></td>


Maybe not possible with DataTable and GridView...


我找到了解决方案...

您开始侦听GridViews RowDataBound事件,该事件对于从源添加到GridView的每一行都触发一次.然后调用Server.HtmlDecode方法并传递字符串,作业就完成了.

示例:

I found a solution...

You start listening for the GridViews RowDataBound event which fires once for each row added to the GridView from the source. Then call the Server.HtmlDecode method and pass the string and the job is done...

Example:

<pre lang="cs">protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = Server.HtmlDecode(e.Row.Cells[0].Text);
        }

    }






这篇关于在DataTable ASP.NET中编写HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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