简体   繁体   English

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

[英]Add hyperlink to gridview programmatically

I have a gridview and need to add controls to header row. 我有一个gridview,需要将控件添加到标题行。 I am able to add text, but how can I add a hyperlink to the header row. 我可以添加文本,但是如何向标题行添加超链接。

 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);
        }
    }

I think you would have to add the control to a specific Cell within the row, eg 我认为您必须将控件添加到行内的特定单元格,例如

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

Since it's a grid, it's not logical for items to exist within a row outside a cell - then there is no way to know where exactly to display it. 由于它是网格,因此将项目存在于单元格外部的行中是不合逻辑的,因此无法知道确切显示在何处。

You probably also need to set the NavigateUrl property of the Hyperlink, otherwise it does not know where to navigate eg 您可能还需要设置Hyperlink的NavigateUrl属性,否则它不知道导航到的位置,例如

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

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

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