How to use a gridview button inside an Accordion RRS feed

  • Question

  • User-2010267424 posted


    I have this code:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <asp:Accordion ID="Accordion1" runat="server"
                            <%--<asp:Label ID="lbHeaderID" runat="server" Text='<%# Eval("InspectionID")%>'></asp:Label>--%>
                            <table style="width:100%">
                                <tr style="width:100%; background-color:lightgray">
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbInspectionDate" Text='<%# Eval("SurveyDate")%>' runat="server"></asp:LinkButton> </td>
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbAuditors" runat="server" Text='<%# Eval("AudLastName")%>'></asp:LinkButton> </td>
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbVP" runat="server" Text='<%# Eval("VPLastName")%>'></asp:LinkButton> </td>
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbManager" runat="server" Text='<%# Eval("ManLastName")%>'></asp:LinkButton> </td>
                                    <td style="width:30.5%"> <asp:LinkButton ID="lbOrg" runat="server" Text='<%# Eval("Organization")%>'></asp:LinkButton> </td>
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbFac" runat="server" Text='<%# Eval("Facility")%>'></asp:LinkButton> </td>
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbDeficiency" runat="server" Text='<%# Eval("Deficiencies")%>'></asp:LinkButton> </td>
                                    <td style="width:9.9%"> <asp:LinkButton ID="lbCompleted" runat="server" Text='<%# Eval("Active")%>'></asp:LinkButton> </td>
                            <asp:HiddenField ID="hfInspectionID" runat="server" value='<%# Eval("InspectionID")%>' />
                            <table style="width:100%">
                                <tr style="width:100%">
                                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/delete2.jpg" /><br />
                                        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/250_PPT8800.jpg" />
                             <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
                                    <asp:BoundField DataField="Severity" HeaderText="Class" SortExpression="DeficiencyID" />
                                    <asp:BoundField DataField="Deficiency" HeaderText="Deficiency" SortExpression="ViolationID" />
                                    <asp:BoundField DataField="Completed" HeaderText="Completed" SortExpression="DeficiencyID" />
                                    <asp:BoundField DataField="DeficiencyID" HeaderText="Completed?" SortExpression="DeficiencyID" />
                                    <asp:ButtonField CommandName="Details_Click" HeaderText="Details" Text="Click Here" />
                                    <asp:TemplateField >
                                <asp:ImageButton  ID="btnViewDetails" runat="server" OnClick="btnViewDetails_Click" ImageUrl='<%#"~/images/250_PPT8800.jpg"%>' /> 
                            <ItemStyle HorizontalAlign="Right" />

    As you can see I have 2 buttons one is an image button the other is a gridview ButtonField button. In the code behind I have this code:


    Protected Sub btnViewDetails_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
            Dim ss As String
            Dim GridView1 As GridView = CType(Me.Accordion1.FindControl("GridView1"), GridView)
            Dim btnDetails As ImageButton = TryCast(sender, ImageButton)
            Dim row As GridViewRow = DirectCast(btnDetails.NamingContainer, GridViewRow)
            ss = row.Cells(3).Text
        End Sub
    This works great when I run the code and click on the Image Button inside the grid the code behind fires and does what it is suppose to do.
    My question is how do I make code behind that will capture the other button(gridview ButtonField button) click event?
    Also can I put a button in the template field? and if so how to I make the code behind capture that click event?

    Thank you

    Tuesday, October 7, 2014 8:49 PM


  • User1918509225 posted

    Hi mutlyp,

    First ,if you want to fire the  click the event of the ButtonField in gridview,you need to set  a commandname for your ButtonField like below:

    <asp:buttonfield buttontype="Button" 
                headertext="Select Customer" 

    Then  you need to add OnRowCommand event in your gridview  to handler the ButtonField Event:

    void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    ...// } }

    You can refer to the link for more detailed information:



    Second,If you want to fire the button click in the itemtempate ,you can also set CommandName like above,please refer to the link which explain how to fire the button click event in the ItemTemplate:



    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 8, 2014 9:39 PM