2012-11-27 63 views
0

我有三個轉發器以父子關係(所以我們有一個父中繼器,和孩子中繼器,和一個孩子,孩子中繼器)與最內轉發未觸發。這裏是我的aspx頁面佈局:嵌套Repeater

<asp:Repeater ID="rptMission" runat="server"> 
<HeaderTemplate> 
<ul id="acc1" class="ui-accordion-container"> 
</HeaderTemplate> 
    <ItemTemplate> 
<li> 
    <div class="ui-accordion-left"></div> 
    <a class="ui-accordion-link acc1"><%# Eval("Name") %><span class="ui-accordion-right"></span></a> 
    <div> 
     <ul class="ui-accordion-container" id="acc2"> 
     <asp:Repeater ID="rptActivity" runat="server"> 
     <ItemTemplate> 
      <li> 
       <div class="ui-accordion-left"></div> 
       <a class="ui-accordion-link acc2"><%# Eval("Name") %> 
       <span class="ui-accordion-right"></span></a> 
       <div> 
       <asp:Repeater ID="rptProject" runat="server"> 
       <ItemTemplate> 
        <%# Eval("Name") %><br/> 
       </ItemTemplate> 
       </asp:Repeater> 
       </div> 
      </li> 
     </ItemTemplate> 
     </asp:Repeater> 
     </ul> 
    </div> 
</li> 
</ItemTemplate> 
<FooterTemplate> 
</ul> 
</FooterTemplate> 
</asp:Repeater> 

前兩個中繼器工作很好,但'rptProject'中繼器不會被觸發。這裏是我的代碼背後:

protected void Page_Load(object sender, EventArgs e) 
    { 
     _presenter = new TierTypesPresenter(this); 
     rptMission.ItemDataBound += new RepeaterItemEventHandler(rptMission_ItemDataBound); 

     RaiseStartUp(); 
    } 

    void rptMission_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
    { 
     RepeaterItem item = e.Item; 
     Mission row = (Mission)item.DataItem; 
     if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
     { 
      var rptActivity = (Repeater)item.FindControl("rptActivity"); 
      var activity = _presenter.GetActivitiesByMission(row.Id); 
      rptActivity.DataSource = activity; 
      rptActivity.DataBind(); 
     } 
    } 

    void rptActivity_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
    { 
     RepeaterItem item = e.Item; 
     Activity row = (Activity)item.DataItem; 
     if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
     { 
      var rptProject = (Repeater)item.FindControl("rptProject"); 
      var project = _presenter.GetProjectsByActivities(row.Id); 
      rptProject.DataSource = project; 
      rptProject.DataBind(); 
     } 
    } 

    public void SetMissions(IEnumerable<Mission> missionList) 
    { 
     rptMission.DataSource = missionList; 
     rptMission.DataBind(); 
    } 

如果我能觸發第二ItemDataBound事件「rptActivity_ItemDataBound」我認爲這是可行的罰款,但它被跳過。感謝您的任何見解!

回答

1

我通過將第三個解決我的問題中繼器綁定到第二個中繼器ItemBoundEvent。以下是任何感興趣的更新代碼:

void rptMission_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
    { 
     RepeaterItem item = e.Item; 
     Mission row = (Mission)item.DataItem; 
     if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
     { 
      var rptActivity = (Repeater)item.FindControl("rptActivity"); 
      var activity = _presenter.GetActivitiesByMission(row.Id); 
      var i = 0; 
      foreach (Activity data in activity) 
      { 
       RepeaterItem activityItem = rptActivity.Items[i]; 
       var rptProject = (Repeater)activityItem.FindControl("rptProject"); 
       var project = _presenter.GetProjectsByActivities(data.Id); 
       rptProject.DataSource = project; 
       rptProject.DataBind(); 
       i++; 
      } 
      rptActivity.DataSource = activity; 
      rptActivity.DataBind(); 
     } 
    } 
1

我猜你缺少這行代碼:

rptActivity .ItemDataBound += new RepeaterItemEventHandler(rptActivity_ItemDataBound); 

這是比較容易設置事件順便把asp文件...

+0

我收到'未設置爲對象實例的引用'錯誤。 – Omni

+0

我設置在rptActivty控制rptActivity_ItemDataBound的OnDataBound事件,但我得到一個錯誤說它不包含定義它。思考? – Omni

+1

@ negzero7那是因爲你的rptActivity_ItemDataBound是私人的。使其受到保護,它將起作用。 MHD。 Yasseen的答案是要走的路。你不應該在同一個處理程序中填充代碼。 – jbl