【问题标题】:Add hyperlink to gridview programmatically以编程方式将超链接添加到gridview
【发布时间】:2018-06-19 19:28:44
【问题描述】:

我有一个gridview,需要向标题行添加控件。我可以添加文本,但是如何向标题行添加超链接。

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell HeaderCell = new TableCell();

            HeaderCell.Text = "Logistics Details";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);
            GridView1.Controls[0].Controls.AddAt(0, HeaderGridRow);

            HyperLink hl = new HyperLink();
            hl.ID = "hlDetail";
            hl.Text = "Details";
            //the below line doesn't add controls
            HeaderGridRow.Controls.Add(hl);
        }
    }

【问题讨论】:

    标签: c# asp.net .net gridview


    【解决方案1】:

    我认为您必须将控件添加到行中的特定单元格,例如

    HeaderGridRow.Cells[0].Controls.Add(hl);
    

    由于它是一个网格,因此项目存在于单元格外的一行中是不合逻辑的 - 那么就无法知道在哪里显示它。

    您可能还需要设置超链接的NavigateUrl 属性,否则它不知道例如导航到哪里

    hl.NavigateURL = "https://www.google.com"
    

    【讨论】:

      猜你喜欢
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2019-03-20
      • 2013-09-09
      相关资源
      最近更新 更多