AccordionItem.FindControl()返回null

时间:2012-08-16 15:11:04

标签: asp.net ajaxcontroltoolkit

好吧通常当我尝试在ItemDatabound上访问说ListView的元素时,它就像说e.Item.FindControl(“myControl”)一样简单,但这对我来说似乎不适合使用来自的Accordion ajaxtoolkit。

这是标记:

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

这是OnItemDataBound事件的代码隐藏:

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

当我单步执行代码时,item正确获取已加载的项目。但是,innerList将返回null。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

检查ItemType类的AccordionItemEventArgs属性。

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