Repeater嵌套Repeater的结构
cs代码:
private void RpTypeBind()
{//GetQuestionTypeAndCount() 返回一个datatable
this.rptypelist.DataSource = LiftQuestionCtr.GetQuestionTypeAndCount();
this.rptypelist.DataBind();
}
protected void rptypelist_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,
//HeaderTemplate,,ItemTemplate,SeparatorTemplate)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("rpquestionlist") as Repeater;//找到里层的repeater对象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
int typeid = Convert.ToInt32(rowv["qtid"]); //获取填充子类的id
rep.DataSource = LiftQuestionCtr.GetSomeQuestionsByTypeid(typeid, 2);
rep.DataBind();
}
}
aspx:
<table border="0" cellpadding="0" cellspacing="0" style="margin-bottom: 5px" width="100%">
<asp:Repeater runat="server" ID="rptypelist" OnItemDataBound="rptypelist_ItemDataBound">
<ItemTemplate>
<tr>
<td class="fb">
<a href="Lists.aspx?Qtid=<%#Eval("qtid") %>"><%#Eval("qtypename") %></a> <span id="askCount_15"
class="date2">[<%#Eval("typecount")%>]</span>
</td>
</tr>
<tr>
<td>
<div id="askTop2_15" class="ask_list">
<asp:Repeater runat="server" ID="rpquestionlist">
<ItemTemplate>
<a href="Question.aspx?qid=<%#Eval("liftquestionid")%>">
<%#Eval("questiontitle")%></a><br />
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
在一般的网站中浏览类别的用户控件通常都位于大多数 ASP.NET 页的左边,它使用户能够按类别快速的查找产品。最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加进去。一来更方便,二来加长了左部导航栏的长度使页面更协调。原有的分类导航栏是由Repeater实现的,现在需要在每一个分类下加入该类的商品信息,于是我想到了在原有Repeater中嵌套Repeater。实现界面如下:
前台页面部分:
<asp:Repeater id="rptCategories" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<!--分类名称-->
<tr><th><%# DataBinder.Eval(Container.DataItem, "TypeName") %></th></tr>
<!--分类下的产品-->
<asp:Repeater id="rptProduct" runat="server">
<ItemTemplate>
<tr><td><a href='ProductInfo.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "ID") %>'>
<%# DataBinder.Eval(Container.DataItem, "ProductName") %></a></td></tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台代码部分(部分代码):
//在绑定分类品名时,绑定分类下的产品
private void rptCategories_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
BLL.Products products =new BLL.Products();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int CategorieId = Convert.ToInt32(rowv["ID"]);
//根据分类ID查询该分类下的产品,并绑定产品Repeater
rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId);
rptProduct.DataBind();
}
}
摘自 http://www.webshu.net/jiaocheng/programme/ASPNET/200804/12053.html