locked
How to use a gridview button inside an Accordion RRS feed

  • Question

  • User-2010267424 posted

    Hello,

    I have this code:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Accordion ID="Accordion1" runat="server"
                        SuppressHeaderPostbacks="true"
                        SelectedIndex="0"
                        HeaderCssClass="header"
                        HeaderSelectedCssClass="headerSelected"
                        ContentCssClass="contentAcc"
                        AutoSize="None"
                        FadeTransitions="true"
                        TransitionDuration="100">
                        <HeaderTemplate>
                            <%--<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>
                                </tr>
                            </table>
    
    
                        </HeaderTemplate>
                        <ContentTemplate>
                            <asp:HiddenField ID="hfInspectionID" runat="server" value='<%# Eval("InspectionID")%>' />
                            <table style="width:100%">
                                <tr style="width:100%">
                                    <td>
                                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/delete2.jpg" /><br />
                                        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/250_PPT8800.jpg" />
                                    </td>
                                    <td>
                             <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
                                <Columns>
                                    <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 >
                            <ItemTemplate>                           
                                <asp:ImageButton  ID="btnViewDetails" runat="server" OnClick="btnViewDetails_Click" ImageUrl='<%#"~/images/250_PPT8800.jpg"%>' /> 
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Right" />
                        </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                                    </td>
                                </tr>
                            </table>
                            
                        </ContentTemplate>
                    </asp:Accordion>
    
                </ContentTemplate>
            </asp:UpdatePanel>

    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

Answers

  • 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" 
                commandname="Select"
                headertext="Select Customer" 
                text="Select"/>
    

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

    void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
    if(e.CommandName=="Select")
          {
    ...// } }

    You can refer to the link for more detailed information:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname(v=vs.110).aspx

     

    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:

    http://msdn.microsoft.com/en-us/library/vstudio/bb907626(v=vs.100).aspx

     

    Best Regards,

    Kevin Shen.

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