前台代码:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound">
<ItemTemplate>
<p>
<a href='test.aspx?Category=<%# Eval("Id") %>'><%# Eval("Id") %></a>
</p>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<li><a href='<%# Eval("href") %>'>
<%# Eval("text")%></a></li>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
使用的类结构是这样的,Category类包含了一个Link类的集合
public class Cateogory
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Id
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<Link> Links
{ get; set; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Link
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string href
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string text
{ get; set; }
}
为了演示,不从数据库读取,自己填充一些数据
private static List<Cateogory> GetData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
List<Cateogory> categories = new List<Cateogory>();
for (int i = 0; i < 5; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Cateogory c = new Cateogory();
c.Id = i;
List<Link> lst = new List<Link>();
for (int j = 0; j < 3; j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Link l = new Link();
l.href = "test.aspx?Category=" + i + "&Id=" + j;
l.text = "Cateogory:" + i + ";Id:" + j;
lst.Add(l);
}
c.Links = lst;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
categories.Add(c);
}
return categories;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Page_Load的时候绑定Repeater1
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
List<Cateogory> categories = GetData();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Repeater1.DataSource = categories;
Repeater1.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
关键在这里了,Repeater1绑定的时候,方法里要获取子类的集合,如果是数据库,应该是获取Id,再从数据库中根据Id取数据列表.
protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Repeater rpt = e.Item.FindControl("Repeater2") as Repeater;
if (rpt != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Cateogory category = e.Item.DataItem as Cateogory;
rpt.DataSource = category.Links;
rpt.DataBind();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后的效果如图:
![](https://www.cnblogs.com/images/cnblogs_com/yvesliao/RepeaterNested.jpg)