[英]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.