繁体   English   中英

以编程方式将超链接添加到gridview

[英]Add hyperlink to gridview programmatically

我有一个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);
        }
    }

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

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

由于它是网格,因此将项目存在于单元格外部的行中是不合逻辑的,因此无法知道确切显示在何处。

您可能还需要设置Hyperlink的NavigateUrl属性,否则它不知道导航到的位置,例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM