网格视图超链接字段可见性由另一个相应的列内容确定



我正在努力寻找解决问题的方法,我花了大量时间尝试替代解决方案,但无济于事。任何帮助或解释将不胜感激。

任务

我需要使 asp.net Gridview 中的超链接字段仅在安全列设置为"受限"时可见/可单击。

电流输出

https://i.stack.imgur.com/v6N79.jpg

《守则》

<asp:HyperLinkField DataNavigateUrlFields="ReportID, Reference_Num, Title, Description"
DataNavigateUrlFormatString="ReportRequest.aspx?ID={0}&Title={2}&Description={3}"
Text="Request Access" Visible='<%#Eval("Security").ToString()=="Unrestricted"?False:True %>' />

如您所见,我正在尝试将超链接字段的可见属性与 Eval 一起使用,以读取安全列中的相应文本。

有什么想法吗?

谢谢。

您可以切换到具有超链接控件的模板字段,并在其中设置可见性。

<asp:TemplateField HeaderText="Ticket Number">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Visible='<%# Eval("Security").ToString() == "Security" ? false : true %>'>Request Access</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

或者使用 RowDataBound 事件在正确的单元格中找到生成的超链接,并设置代码隐藏的可见性。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//locate the hyperlink in the correct cell nummer. It is always the first control in the cell
HyperLink hl = e.Row.Cells[colIndex].Controls[0] as HyperLink;
//validate the value
if (row["Security"].ToString() == "Security")
{
hl.Visible = false;
}
}
}

相关内容

最新更新