2012-08-16 47 views
0

好吧,所以通常當我嘗試訪問ItemDatabound上的一個ListView的元素時,它就像說e.Item.FindControl(「myControl」)一樣簡單,但這似乎不是爲我工作使用ajaxtoolkit的Accordion。AccordionItem.FindControl()返回null

這裏的標記:

<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將返回爲空。有任何想法嗎?

回答

1

檢查AccordionItemEventArgs類的ItemType屬性。

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

工作。有什麼機會可以解釋爲什麼這是?對我來說似乎很奇怪。 – Sinaesthetic 2012-08-16 16:05:48