views:

1636

answers:

4

I have the following structure in an aspx page:

<asp:Panel ID="pnlCust" runat="server">
    <asp:GridView ID="gvMaster" runat="server" 
                  OnRowCreated="gvMaster_RowCreated">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Panel ID="pnlMaster" runat="server">
                        //...
                    </asp:Panel>
                    <asp:Panel ID="pnlDetails" runat="server">
                        <asp:GridView ID="gvDetails" runat="server">
                            <Columns>
                                //...
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</asp:Panel>

The panels are used for the CollapsiblePanelExtender from the Ajax Control Toolkit.

I am trying to use FindControl to find the gvDetails control in code-behind, but my latest attempt has not worked, which is in the gvMaster_RowCreated event:

GridView gv =  
e.Row.FindControl("pnlDetails").FindControl("gvDetails") as GridView;

where e is GridViewRowEventArgs

I am basically doing what was on this page, but I am not using a SqlDataSource, however, the person is basically finding the SqlDataSource via the FindControl from the e argument passed in from the RowCreated event. Here is the link:

http://mosesofegypt.net/post/2008/02/Building-a-grouping-Grid-with-GridView-and-ASPNET-AJAX-toolkit-CollapsiblePanel.aspx

+1  A: 

I don't believe that a GridView can have child controls like that.

John Saunders
Which child controls are you talking about?
Xaisoft
Look at my edited question.
Xaisoft
A: 

You need to put the panel inside a template field. You can try something like this:

<asp:Panel ID="pnlCust" runat="server"> 
    <asp:GridView ID="gvMaster" runat="server" >
        <columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Panel ID="pnlDetails" runat="server">
                        <asp:GridView ID="gvDtails" runat="server">
                            <columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <%-- columns here --%>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </columns>
                        </asp:GridView> <%-- end gvDetails --%>
                    </asp:Panel> <%-- end pnlDetails--%>
                </ItemTemplate>
            </asp:TemplateField>
        </columns>
    </asp:GridView> <%-- end gvMaster --%>
</asp:Panel> <%-- end pnlCust --%>
Yes, this is actually how the code is. I just took it out for more clarity and now I am trying to find the gvDetails GridView in Code Behind. I will update my question with the template field.
Xaisoft
+1  A: 

Here is a code behind method which worked for me:

protected void gvMaster_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        GridView gv = (GridView)e.Row.FindControl("gvDetails");
    }
}

Is your app throwing an exception? What is not working properly?

I tried that, like you have above, but I did it the following way which returned a null value:GridView gv = e.Row.FindControl("gvDetails") as GridView;
Xaisoft
I tried your snippet above and when I step through, gv is null.
Xaisoft
I guess I'm stumped. Sorry! Only thing left I would try is using the RowDataBound event instead of RowCreated but it's probably a long shot.
Nate, Sorry, you were right. Stupid me had the spelling of the GridView ID as gvDtails in the markup, so that is why it wasn't finding it. Thanks!
Xaisoft
A: 

Hello Xaisoft, did you find the solution how to implement that?