我试图从数据库中获取值并将其显示在Gridview中,就像表格一样,问题是,我想创建最后一行,即File Row成为可以重定向到其他webforms的超链接。我尝试使用下面的代码,它无法工作,任何关于如何动态添加超链接的线索?我不希望它是等标签,因为我不知道数据库中有多少数据,提前谢谢。
sqlDr = command.ExecuteReader();
DataTable dataTable = new DataTable();
DataColumn dc = new DataColumn("ID", typeof(string));
DataColumn dc1 = new DataColumn("Name", typeof(string));
DataColumn dc2 = new DataColumn("Extension", typeof(string));
DataColumn dc3 = new DataColumn("File", typeof(string));
dataTable.Columns.Add(dc);
dataTable.Columns.Add(dc1);
dataTable.Columns.Add(dc2);
dataTable.Columns.Add(dc3);
//here
while (sqlDr.Read())
{
TableCell tc = new TableCell();
DataRow dr = dataTable.NewRow();
dr["ID"] = sqlDr["DOCID"].ToString();
dr["Name"] = sqlDr["FILENAME"].ToString();
dr["Extension"] = sqlDr["GENERATEDDOCID"].ToString();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink link = new HyperLink();
link.ID = sqlDr["DOCID"].ToString();
link.NavigateUrl = "members.aspx?showLease=";
e.Row.Cells.Add(link);
}
dataTable.Rows.Add(dr);
}
downloadFilesTable.DataSource = dataTable;
downloadFilesTable.DataBind();
con.Close();
答案 0 :(得分:2)
你可以这样做。循环GridView中最后一行的每个单元格并添加HyperLink。
foreach (TableCell cell in GridView1.Rows[GridView1.Rows.Count -1].Cells)
{
HyperLink hl = new HyperLink();
hl.NavigateUrl = "members.aspx?showLease=" + cell.Text;
hl.Text = cell.Text;
cell.Controls.Add(hl);
}
或者您可以使用JavaScript点击整个行。
GridView1.Rows[GridView1.Rows.Count - 1].Attributes.Add("onclick", "location.href='/members.aspx?showLease='");
GridView1.Rows[GridView1.Rows.Count - 1].Attributes.Add("style", "cursor:pointer;");