Gridview HyperlinkField 字段可见性由另一个对应的列内容决定

Gridview HyperlinkField field visibility determined by another corresponding columns contents

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

任务

如果安全列设置为受限,我只需要在 asp.net Gridview 中创建我的超链接字段 visible/clickable。

当前输出

https://imgur.com/a/h8mqh

代码

<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 %>' />

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

有什么想法吗?

谢谢。

您可以切换到带有超链接控件的 TemplateField 并在那里设置可见性。

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