question

HB-3362 avatar image
0 Votes"
HB-3362 asked HB-3362 edited

How can I make linkbutton visible in Gridview?

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

dotnet-csharpdotnet-aspnet-generaldotnet-aspnet-webformsdotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

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#)


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HB-3362 avatar image
0 Votes"
HB-3362 answered HB-3362 edited

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;
             }
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.