0

好的,通常当我尝试访问 ItemDatabound 上的 ListView 的元素时,就像说 e.Item.FindControl("myControl") 一样简单,但这似乎对我使用 ajaxtoolkit 中的 Accordion 不起作用。

这是标记:

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

当我单步执行代码时,项目正在正确获取加载的项目。但是,innerList 将返回为空。有任何想法吗?

4

1 回答 1

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();
    }
}
于 2012-08-16T15:37:47.893 回答