How can I make linkbutton visible in Gridview?

HB 1 Reputation point
2022-07-25T11:50:05.677+00:00

The OnClick="lnkRelease_Click" does not change the boolean values for my linkbutton controls in the itemtemplate.

Any help on pointing me to the right direction, would be appreciated.

My ASP page:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id,InsertDate" DataSourceID="HRD" ShowFooter="True" HorizontalAlign="Center">  
        <Columns>  
            <asp:TemplateField ShowHeader="False">  
                <FooterTemplate>  
                    <asp:LinkButton ID="lnkInsert" runat="server" CssClass="button" OnClick="lnkInsert_Click" ValidationGroup="Insert">Add</asp:LinkButton>  
                    <asp:LinkButton ID="LnkDeduct" runat="server" OnClick="LnkDeduct_Click" ValidationGroup="Insert">Deduct</asp:LinkButton>  
                </FooterTemplate>  
                <ItemTemplate>  
                    <asp:LinkButton ID="lnkRelease" runat="server" Visible="true" Text="Release" OnClick="lnkRelease_Click"></asp:LinkButton>  
                    <asp:LinkButton ID="lnkLock" runat="server" Visible="false" Text="Lock"></asp:LinkButton>&nbsp;  
                    <asp:LinkButton ID="lnkDel" runat="server" Visible="false"  
                        CommandName="Delete" OnClientClick="return confirm('Are you sure you want to Delete?');" Text="Delete" OnClick="lnkDel_Click"></asp:LinkButton>  
                </ItemTemplate>  
                <ItemStyle Width="100px" Wrap="False" />  
            </asp:TemplateField>  

And my code page for the OnClick event:

        protected void lnkRelease_Click(object sender, EventArgs e)  
        {  
          
            foreach (GridViewRow row in GridView1.Rows)  
            {  
                ((LinkButton)row.FindControl("lnkLock")).Visible = true;  
                ((LinkButton)row.FindControl("lnkDel")).Visible = true;  
                ((LinkButton)row.FindControl("lnkRelease")).Visible = false;  
            }  
  
        }  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,243 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,196 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,181 Reputation points
    2022-07-25T15:17:28.34+00:00

    The standard GridView pattern is link buttons are associated with a Gridview template; Item template, Edit template, etc. Toggling the GridView templates shows the associated link button(s) for that template. I recommend going through the following tutorial.

    Adding and Responding to Buttons to a GridView (C#)

    0 comments No comments

  2. HB 1 Reputation point
    2022-07-25T17:01:05.683+00:00

    Thanks,

    I had figured it out to use the RowCommand instead:

            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
            {  
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);  
      
                if (e.CommandName == "Release")  
                {  
                    ((LinkButton)row.FindControl("lnkLock")).Visible = true;  
                    ((LinkButton)row.FindControl("lnkDel")).Visible = true;  
                    ((LinkButton)row.FindControl("lnkRelease")).Visible = false;  
                }  
                else   
                {  
                    ((LinkButton)row.FindControl("lnkLock")).Visible = false;  
                    ((LinkButton)row.FindControl("lnkDel")).Visible = false;  
                    ((LinkButton)row.FindControl("lnkRelease")).Visible = true;  
                }  
            }  
    
    0 comments No comments