1

可以在另一个中继器内使用中继器吗?如何?

我的问题是,后端看不到中继器内的中继器。

正面:

<div id="blogright">
    <ul class="accordion">
          <asp:Repeater ID="parentRep" runat="server">
             <ItemTemplate>
                    <li class="2010">
                    <a href="blog.aspx?id=<%# Eval("DYear") %>"><%# Eval("DYear") %><span><%# Eval("PCount") %></span></a>
                   <ul class="sub-menu">
                     <asp:Repeater ID="childRep" runat="server">
                        <ItemTemplate>
                            <li><a href="#"><em><%# Eval("DDay") %></em><%# Eval("DMonth") %><span><%# Eval("ICount") %></span></a></li>    
                        </ItemTemplate>
                     </asp:Repeater>
                  </ul>
               </li>
            </ItemTemplate>
         </asp:Repeater>
     </ul>
</div>

背部:

//This is for parentRep
 daString = "SELECT datepart(YEAR,BLG_DATE) as DYear,COUNT(BLG_DATE) as PCount FROM [BLG] INNER JOIN [ACC] ON [BLG].ACC_ID=[ACC].ACC_ID WHERE [BLG].ACC_ID='"+userID+"' GROUP BY BLG_DATE";
 SqlDataAdapter da2 = new SqlDataAdapter(daString, conn);
 DataTable dt2 = new DataTable();
 da2.Fill(dt2);

 parentRep.DataSource = dt2;
 parentRep.DataBind();

//This is for childRep
 dt.Clear();
 daString = "SELECT DATEPART(DAY,BLG_DATE) as DDay,datename(month,BLG_DATE) as DMonth,DATEPART(YEAR,BLG_DATE) as DYear FROM [BLG] INNER JOIN [ACC] ON [BLG].ACC_ID=[ACC].ACC_ID WHERE [BLG].ACC_ID='"+userID+"' and BLG_DATE like '%"+urlId+"%'";
 SqlDataAdapter da3 = new SqlDataAdapter(daString, conn);
 DataTable dt3 = new DataTable();
 da2.Fill(dt3);

//can't see the childRep here.

对不起我的英语不好。

4

1 回答 1

2

您可以通过以下方式在 ItemDataBound 事件中获取对子中继器的引用并将数据绑定到它:

protected void parentRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
   {
        Repeater childRepeater = (Repeater)e.Item.FindControl("childRep");
        childRepeater.ItemDataBound += new RepeaterItemEventHandler(childRepeater_ItemDataBound);
        childRepeater.ItemCommand += new RepeaterCommandEventHandler(childRepeater_ItemCommand);
        childRepeater.DataSource = dt3; //dt3 is the DataTable from your code sample
        childRepeater.DataBind();
   }
}

此外,这个线程中有一些非常彻底的答案:Repeater in Repeater

于 2013-08-02T00:22:38.177 回答