欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Repeater中嵌套Repeater的示例介绍

程序员文章站 2024-02-25 22:57:03
复制代码 代码如下: protected void page_load(object sender, eventargs e) { if (!page.ispostback...
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
this.dtcategory = getcategorytable();
this.dtproduct = getproducttable();
rptcategorylist.datasource = dtcategory;
rptcategorylist.databind();
}
}
// 准备一张分类表
datatable getcategorytable()
{
datatable dt = new datatable();
dt.columns.add("categoryid", typeof(int));
dt.columns.add("categorytitle", typeof(string));
for (int i = 1; i <= 3; i++)
{
datarow row = dt.newrow();
row["categoryid"] = i;
row["categorytitle"] = "分类名字 " + i + "";
dt.rows.add(row);
}
return dt;
}
// 准备一张产品表
datatable getproducttable()
{
datatable dt = new datatable();
dt.columns.add("producttitle", typeof(string));
dt.columns.add("categoryid", typeof(int));
for (int i = 1; i <= 9; i++)
{
datarow row = dt.newrow();
row["producttitle"] = "产品名字 " + i + "";
if (i > 6) row["categoryid"] = 3;
else if (i > 3) row["categoryid"] = 2;
else row["categoryid"] = 1;
dt.rows.add(row);
}
return dt;
}
// 获取某个类别的产品
datatable getproducttable(int categoryid)
{
dataview dv = this.dtproduct.defaultview;
dv.rowfilter = " categoryid=" + categoryid + " ";
return dv.totable();
}
protected void rptcategorylist_itemdatabound(object sender, repeateritemeventargs e)
{
if (e.item.itemtype == listitemtype.alternatingitem || e.item.itemtype == listitemtype.item)
{
datarowview drv = (datarowview)e.item.dataitem;
literal ltltitle = (literal)e.item.findcontrol("ltltitle");
ltltitle.text = drv["categorytitle"].tostring();
repeater rptproductlist = (repeater)e.item.findcontrol("rptproductlist");
rptproductlist.datasource = getproducttable(convert.toint32(drv["categoryid"]));
rptproductlist.databind();
}
}
protected void rptproductlist_itemdatabound(object sender, repeateritemeventargs e)
{
if (e.item.itemtype == listitemtype.alternatingitem || e.item.itemtype == listitemtype.item)
{
datarowview drv = (datarowview)e.item.dataitem;
literal ltltitle = (literal)e.item.findcontrol("ltltitle");
ltltitle.text = drv["producttitle"].tostring();
}
}

前台aspx代码
复制代码 代码如下:

<</code>form id="form1" runat="server">
<</code>div>
<</code>asp:repeater id="rptcategorylist" runat="server" onitemdatabound="rptcategorylist_itemdatabound">
<</code>itemtemplate>
<</code>div class="listbox">
<</code>div class="title">
<</code>asp:literal id="ltltitle" runat="server"></</code>asp:literal></</code>div>
<</code>div class="content">
<</code>ul>
<</code>asp:repeater id="rptproductlist" runat="server" onitemdatabound="rptproductlist_itemdatabound">
<</code>itemtemplate>
<</code>li>
<</code>asp:literal id="ltltitle" runat="server"></</code>asp:literal>
</</code>li>
</</code>itemtemplate>
</</code>asp:repeater>
</</code>ul>
</</code>div>
</</code>div>
</</code>itemtemplate>
</</code>asp:repeat</</code>div>
</</code>form>