如何在中继器内使用中继器

时间:2013-08-01 23:49:08

标签: c# asp.net

可以在另一个中继器内使用中继器吗?怎么样?

我的问题是,后端无法在转发器内看到转发器。

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.

抱歉我的英文。

1 个答案:

答案 0 :(得分:2)

您可以获取对子Repeater的引用,并以这种方式在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