Search code examples
asp.netajaxcontroltoolkit

AccordionItem.FindControl() is returning null


Ok so normally when I try to access elements of say a ListView on ItemDatabound, it's as simple as saying e.Item.FindControl("myControl") but this doesn't seem to be working for me using the Accordion from the ajaxtoolkit.

Here's the markup:

    <ajaxToolkit:Accordion runat="server" ID="accOuterAccordion" OnItemDataBound="accOuterAccordion_ItemDataBound">
        <HeaderTemplate>
            <asp:Label runat="server" Text='<%#Eval("Header") %>'/>
        </HeaderTemplate>
        <ContentTemplate>
            <asp:ListView runat="server" ID="lvReviewers" ItemPlaceholderID="phReviewer">
                <LayoutTemplate>
                    <ul>
                        <asp:PlaceHolder runat="server" ID="phReviewer"/>
                    </ul>
                </LayoutTemplate>
                <ItemTemplate>
                    <li>
                        <asp:Label runat="server" ID="lblReviewer" Text='<%#Eval("Assignee.Name") %>'/>
                    </li>
                </ItemTemplate>
            </asp:ListView>
        </ContentTemplate>
    </ajaxToolkit:Accordion>

And here's the codebehind for the OnItemDataBound event:

   protected void accOuterAccordion_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
    {
        var item = e.AccordionItem.DataItem as MocRequest;
        var innerList = e.AccordionItem.FindControl("lvReviewers") as ListView;
        innerList.DataSource = MocApi.GetReviews(item.MocRequestID);
        innerList.DataBind();
    }

When I step through the code, item is getting the loaded item correctly. However, innerList is coming back as null. Any ideas?


Solution

  • Check for the ItemType property of AccordionItemEventArgs class.

    void Accordion1_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
    {
        if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
        {
            var item = e.AccordionItem.DataItem as MocRequest;
            var innerList = e.AccordionItem.FindControl("lvReviewers") as ListView;
            innerList.DataSource = MocApi.GetReviews(item.MocRequestID);
            innerList.DataBind();
        }
    }