【发布时间】:2018-06-24 21:18:34
【问题描述】:
我有一个动态填充列的网格。我有一个名为 ID 的列,它将启用超链接,如果单元格值为 null 或为空,则需要禁用超链接。
例如:
如果单元格值返回 0 或该 ID 列的空值,那么我需要禁用该列中这些单元格的超链接。下面的代码成功添加了超链接,最重要的是我需要检查单元格中的null 或0 值。
代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string strHeaderRow = GridView1.HeaderRow.Cells[i].Text;
if (strHeaderRow == "ID")
{
string strMktURL = "http://www.address.com";
HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);
}
}
}
}
protected HyperLink AddHyperLink(TableCell cell, string strURL)
{
HyperLink hl = new HyperLink();
hl.Text = cell.Text;
hl.Font.Underline = true;
hl.Target = "_blank";
hl.NavigateUrl = strURL;
hl.Attributes.Add("style", "color:Black;");
cell.Controls.Add(hl);
return hl;
}
请建议如何实现。
【问题讨论】:
-
你的代码没有做什么你想要的?它现在做什么?目前尚不清楚您要问什么以及您想要实现什么(例如,“禁用”超链接,或者如果值为空白/空,则不将超链接放入该单元格?)。请编辑问题以使其更清楚。
-
@Rory - 更新了我的帖子。请看一看。