【问题标题】:Retaining Gridview hyperlink column after postback回发后保留 Gridview 超链接列
【发布时间】:2019-08-18 23:10:45
【问题描述】:

我在后面的代码 (gridView_RowDataBound) 中创建了一个超链接 Gridview 列,该列在 postabk 上变成纯文本。超链接的文本和NavigateUrl是动态生成的gridview的cell[0]的值。

` protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink link = new HyperLink();
                link.Target = "blank";
                link.Text = e.Row.Cells[0].Text;
                link.NavigateUrl = e.Row.Cells[0].Text;
                e.Row.Cells[0].Controls.Add(link);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }`

我正在尝试对 RowCreated 事件执行相同的操作,以确保在回发时保留超链接。但是 Gridview 中的数据丢失了,所以我不能以同样的方式访问它。

【问题讨论】:

  • 将数据绑定到GridView时去掉IsPostBack的勾选。
  • @VDWWD 数据绑定 Gridview 时没有 IsPostBack。每个 postBack 都会调用 RowCreated,所以我尝试使用它来重新创建超链接。

标签: c# asp.net hyperlink postback


【解决方案1】:

对于面临同样问题的任何人,这就是我最终在每次加载页面时所做的事情(postBack)

protected void GridViewDisplayDocument_RowCreated()
{
   var rowCount = GridViewDisplayDocument.Rows.Count;
   for (int i = 0; i < rowCount; i++)
   {
      var url = GridViewDisplayDocument.Rows[i].Cells[0].Text;
      if (url != string.Empty)
      {
         HyperLink link = new HyperLink();
         link.Target = "blank";
         link.Text = url;
         link.NavigateUrl =  url;

         GridViewDisplayDocument.Rows[i].Cells[0].Controls.Add(link);
       }
    } 
} 

【讨论】:

    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多