根据条件启用/禁用Gridview中的链接按钮

时间:2013-08-01 18:20:31

标签: c# asp.net gridview

我有一个gridview gvData我想要的是当TransType Column中的记录等于Dessert然后显示Write,RT。如果它只是显示关闭编辑删除。

关闭编辑删除写入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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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是否有效。测试将决定它。