Enable/Disable Link Buttons in Gridview Depending on Condition

17,082

Solution 1

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

Solution 2

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.

Share:
17,082
Apollo
Author by

Apollo

Updated on June 19, 2022

Comments

  • Apollo
    Apollo almost 2 years

    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>
    

  • Apollo
    Apollo almost 11 years
    I edited your answer, I found the Trans by lblTrans. Please +1 my question
  • mzonerz
    mzonerz over 7 years
    Excelent out of box
  • Si8
    Si8 almost 7 years
    Great answer +1. I edited to ensure the type if matched correctly.