前台页面代码:
<ul>
<asp:Repeater ID="rptClass1" runat="server" OnItemDataBound=" rptClass1_ItemDataBound">
<ItemTemplate>
<li><a href="ProtectList.aspx?ClassID=<%#Eval("ID") %>">
<%#Eval("ClassName") %></a>
<ul>
<asp:Repeater ID="rptClass2" runat="server">
<ItemTemplate>
<li><a href="ProtectList.aspx?ClassID=<%#Eval("ID") %>">
<%#Eval("ClassName") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
后台页面代码 :
public void GetClass()//加载产品一级分类
{
DataTable dt = bllClass.GetDataTable(0, "", "ParentID=13 and IsView=1", "Oid");
this.rptClass1.DataSource = dt.DefaultView;
this.rptClass1.DataBind();
dt.Dispose();
}
protected void rptClass1_ItemDataBound(object sender, RepeaterItemEventArgs e)//添加二级分类
{
// 判断里层的repeater处于哪个外层repeater的哪个位置
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptClass = e.Item.FindControl("rptClass") as Repeater; // 查找内层Repeater并转换到相应类型
DataRowView drv = (DataRowView)e.Item.DataItem; // 找到外层Repeater关联的数据项
long id = long.Parse(drv["ID"].ToString()); // 取得pID
TianDao.BLL.BusinessTD_Base_Class bllClass2 = new TianDao.BLL.BusinessTD_Base_Class();
DataTable dt = bllClass2.GetDataTable(0, "", "ParentID=" + id + " and IsView=1", "Oid");
rptClass.DataSource = dt.DefaultView;
rptClass.DataBind();
dt.Dispose();
}
}
}