html代码按原样出现在c锐利的asp.net中 [英] html code appear as it is in c sharp asp.net

查看:57
本文介绍了html代码按原样出现在c锐利的asp.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见以下代码:

Pleas see to below code:

System.Data.DataTable dt = new System.Data.DataTable();

        dt.Columns.Add(new System.Data.DataColumn("id", typeof(int)));
        dt.Columns.Add(new System.Data.DataColumn("name", typeof(string)));
        dt.Columns.Add(new System.Data.DataColumn("samplenum", typeof(int)));

        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand cmd = new SqlCommand("select * from GRIDTESTDETAILS", conn);
        SqlDataReader dr = cmd.ExecuteReader();

       // DataRow drw = new DataRow();

        TableRow tr = new TableRow();
        while (dr.Read())
        {
            
            string link = "<a href=\"#\" >" + dr[0] + "</a>";
            dt.Rows.Add(dr[0],link, dr[2]);
        }

        gvSample.DataSource = dt; // it is gridview in asp.net
        gvSample.DataBind();

        conn.Close();



通过从数据库中获取数据,代码可以完美执行
但是问题出在以下输出



Code is executing perfectly by fetching data from database
But the problem is below output

id	       name	      samplenum
21	<a href="#" >21</a>	255
22	<a href="#" >22</a>	26
23	<a href="#" >23</a>	26
24	<a href="#" >24</a>	255
25	<a href="#" >25</a>	255


瓦特错了吗?我想在< a>以上作为超链接.
请帮帮我.
谢谢.


Wats going wrong? I want above <a> as Hyperlink.
Please Help me.
Thanks.

推荐答案

好吧,正如您所说的那样,似乎HTML在绑定之前已经被编码了.您需要使用RowDataBound方法对其进行解码.示例:
Ok, as you say, it looks like HTML is getting encoded before binding. You need to decode it in RowDataBound method. Example:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

    if (e.Row.RowType == DataControlRowType.DataRow) {
       e.Row.Cells[columnIndex].Text = Server.HtmlDecode(e.Row.Cells[columnIndex].Text);
    }
}


直接将其绑定,因为该问题即将到来.

如果您选择添加链接按钮,然后与数据网格绑定,您的问题将解决.

如果需要任何信息,请给我发邮件
directly you bind it for that problem is Coming.

if u take a coloumn add link button and then bind with data grid Your Problem will solve.

if need any information just mail me


这篇关于html代码按原样出现在c锐利的asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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