简体   繁体   中英

How to use repeater inside the repeater

It's possible to use repeater inside the another repeater? How?

my problem is, the back-end can't see the repeater inside the repeater.

FRONT:

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

BACK:

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

Sorry for my English.

You can get a reference to the child Repeater and bind data to it in the ItemDataBound event this way:

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

Additionally, there are some very thorough answers in this thread: Repeater in Repeater

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM