如何在asp.net中的gridview中动态添加可点击的HyperLinkField

时间:2017-12-17 03:35:49

标签: c# asp.net

我正在尝试在gridview中动态添加可点击的链接字段。我的代码是

 while (rdr.Read())
        {
            DataRow dr = dtTutorial.NewRow();
            dr["Topic"] = rdr["Topic"];
            dr["Description"] = rdr["Description"];
            HyperLinkField h = new HyperLinkField();
            h.HeaderText = "Visit";
            h.NavigateUrl = "Details.aspx";
            h.DataTextField = rdr["link"].ToString();

            dr["Link"] = h;
            dtTutorial.Rows.Add(dr);
        }

但是当我执行此页面时,我没有获得可点击的文件。我怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

我已经找到了解决方案。代码在这里给出

 while (rdr.Read())
        {
            DataRow dr = dtTutorial.NewRow();
            dr["Topic"] = rdr["Topic"];
            dr["Description"] = rdr["Description"];
            dr["Visit"] = rdr["id"];
            dtTutorial.Rows.Add(dr);
        }
        con.Close();
    }
    GridView1.DataSource = dtTutorial;
    GridView1.DataBind();
    foreach (GridViewRow gr in GridView1.Rows)
    {
        HyperLink hp = new HyperLink();
        hp.Text = "Click here";
        hp.NavigateUrl = "~/Details.aspx?id=" + gr.Cells[2].Text;
        gr.Cells[2].Controls.Add(hp);
    }