Apollo
Apollo

Reputation: 2070

Enable/Disable Link Buttons in Gridview Depending on Condition

I have a gridview gvData what I want is when a record in TransType Column equal to Dessert then show Write, RT. If its anything else the only show Close Edit Delete.

Close Edit Delete Write RT are in a Template Field

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>

Upvotes: 2

Views: 18338

Answers (2)

Garrison Neely
Garrison Neely

Reputation: 3289

In the past I've made a code-behind method that evaluates and returns a boolean.

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

Then in the markup, call that method like so:

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

One thing I can't remember is if IsTransTypeDessert needs to return the string representation of "true" or "false" or if the bool will work. Testing will determine it.

Upvotes: 1

Gloria
Gloria

Reputation: 1053

On your gvData _OnRowDataBound , check for the condition and make the appropriate buttons Visible property to false for each row.

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

Upvotes: 1

Related Questions