根据条件启用/禁用网格视图中的链接按钮

本文关键字:视图 链接 按钮 网格 条件 启用 | 更新日期: 2023-09-27 18:25:48

我有一个网格视图gvData,我想要的是当TransType列中的一条记录等于Dessert时,显示Write,RT。如果有其他内容,则只显示Close Edit Delete。

关闭编辑删除写入RT在模板字段中

ID    TRANSTYPE    R      C     TIME    
1      Dessert   12:00  12:05    12      Close Edit Delete Write RT

<asp:TemplateField ShowHeader="False">
<ItemTemplate>
    <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked" OnClick="CloseClick_Click">Close</asp:LinkButton>
     <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow" OnClick="Edit_Click" CommandArgument='<%# Eval("Id")%>'>Edit</asp:LinkButton>
     <asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="DeleteRow"OnClick="Delete_Click" OnClientClick="return confirm('Are you sure you want to Delete this Transaction?');">Delete ||</asp:LinkButton>
    <asp:LinkButton ID="lbWrite" runat="server" CausesValidation="False" CommandName="WriteClicked" OnClick="Write_Click">Write</asp:LinkButton>
    <asp:LinkButton ID="lbRT" runat="server" CausesValidation="False" CommandName="RT"OnClick="RT_Click">RT</asp:LinkButton>
</ItemTemplate>

根据条件启用/禁用网格视图中的链接按钮

gvData _OnRowDataBound上,检查条件,并使每行的相应按钮Visible属性为false。

            protected void gvData_OnRowDataBound(object sender, GridViewRowEventArgs e)
            {
                LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose");
                LinkButton lbEdit = (LinkButton)e.Row.Cells[5].FindControl("lbEdit");
                LinkButton lbDelete = (LinkButton)e.Row.Cells[5].FindControl("lbDelete");
                LinkButton lbWrite = (LinkButton)e.Row.Cells[5].FindControl("lbWrite");
                LinkButton lbRT = (LinkButton)e.Row.Cells[5].FindControl("lbRT");
                if(e.Row.Cells[1].Text=="Dessert")
                {
                    lbClose.Visible = false;
                    lbEdit.Visible = false;
                    lbDelete.Visible = false;
                }
                else
                {
                    lbWrite.Visible = false;
                    lbRT.Visible = false;
                }
            }

在过去,我制作了一个代码隐藏方法,用于计算并返回布尔值。

protected bool IsTransTypeDessert(string transType)
{
    return transType.ToLower() == "dessert";
}

然后在标记中,调用这样的方法:

<asp:LinkButton ID="lbWrite" runat="server" CausesValidation="False" CommandName="WriteClicked" OnClick="Write_Click"
Visible='<%# IsTransTypeDessert(Eval("TRANSTYPE") != null ? Eval("TRANSTYPE").ToString() : "") %>'>Write</asp:LinkButton>

有一件事我记不清了,那就是IsTransTypeDessert是否需要返回"true"或"false"的字符串表示,或者bool是否有效。测试将决定它。