【问题标题】:Dynamic link in Gridview C#Gridview C# 中的动态链接
【发布时间】:2016-06-27 06:42:19
【问题描述】:

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

我正在寻找可以添加指向 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;

【问题讨论】:

  • 为什么你不使用超链接
  • 你的意思是超链接字段?
  • 是的超链接字段或模板字段内的超链接
  • 我不想再添加一列,只需要将数据源中现有列的值修改为链接格式
  • ok 显示你的代码 html 和隐藏你正在尝试的代码

标签: c# gridview


【解决方案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 更改为您的实际单元格索引号。

【讨论】:

    【解决方案2】:

    您可以使用

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

    我已经试过了,对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2017-11-28
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多