Gridview C#中的动态链接

时间:2016-06-27 06:42:19

标签: c# gridview

我有一个网格视图。数据表是数据源。

我正在寻找可以添加gridview列链接的解决方案。但不是通过创建超链接字段,甚至不想与设计师一起玩。

这是一个常见的gridview,它将获得包括列的动态数据。

没有必要每个数据源都有'名称'作为超链接的目标列。

我能够生成<a href='...'>text</a>,但它在gridview中显示。

HTML阻止:

<asp:GridView ID="gvCommonDashboard" runat="server">  

</asp:GridView>

代码背后:

foreach (DataRow dr in dt.Rows)
{                        
    string name = Convert.ToString(dr["Name"]);

    string url = SPContext.Current.Web.Url + listName + "/" + name;
     dr["Name"] = string.Format("<a href='" + url + "'>" + name + "</a>");

}
dt.AcceptChanges();                                     

gvCommonGrid.DataSource = dt;
gvCommonGrid.DataBind();
gvCommonGrid.Visible = true;

2 个答案:

答案 0 :(得分:1)

您可以在gridview的RowDataBound事件中构建链接 -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[CellIndex].Text);
        e.Row.Cells[CellIndex].Text = decodedText;
    }
}

将CellIndex更改为您的实际单元格索引号。

答案 1 :(得分:0)

您可以使用

格式化字符串
string.Format("<a href='" + fullPath + "'>Text</a>");

我试过这个对我来说很好。