Repeater中嵌套Repeater的示例介绍

 更新时间:2014年01月02日 16:22:31   作者:  
在某些特殊情况下是需要在Repeater中嵌套使用Repeater的,下面有个不错的示例,感兴趣的朋友可以参考下
复制代码 代码如下:

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>

相关文章

  • .NET Core/Framework如何创建委托大幅度提高反射调用的性能详解

    .NET Core/Framework如何创建委托大幅度提高反射调用的性能详解

    反射是一种很重要的技术,下面这篇文章主要给大家介绍了关于.NET Core/Framework如何创建委托大幅度提高反射调用性能的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-09-09
  • asp.net Linq to Xml学习笔记

    asp.net Linq to Xml学习笔记

    之前都没有学习过关于XML文件的操作,由于最近开发的项目需要用到,开始时学习了原始的XML文件操作方法,看了半天,也看的头晕眼花,没学习到真正的用法,后来在同事的推荐下学习了Linq to Xml
    2010-03-03
  • 使用CefSharp在.Net程序中嵌入Chrome浏览器

    使用CefSharp在.Net程序中嵌入Chrome浏览器

    这篇文章介绍了使用CefSharp在.Net程序中嵌入Chrome浏览器的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 在.Net中使用MongoDB的方法教程

    在.Net中使用MongoDB的方法教程

    最近在研究mongodb,在网上搜索发现针对.net 中使用mongodb的文章要么是早期的驱动版本,要么资料很少,所以写个随笔记录一下,本文详细的给大家介绍了在.Net中使用MongoDB的方法教程,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • .Net Core依赖注入IOC和DI介绍

    .Net Core依赖注入IOC和DI介绍

    这篇文章介绍了.Net Core依赖注入IOC和DI,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • 详解Asp.net Core 使用Redis存储Session

    详解Asp.net Core 使用Redis存储Session

    本篇文章主要介绍了Asp.net Core 使用Redis存储Session ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧。
    2016-12-12
  • ASP.NET实现URL映射的方法

    ASP.NET实现URL映射的方法

    本文介绍ASP.NET中的URL映射,你可以用来在ASP.NET中干净地映射或重写URL,以及按照你自己的需求组织你的URL的结构。
    2016-04-04
  • 如何在.Net6 web api中记录每次接口请求的日志

    如何在.Net6 web api中记录每次接口请求的日志

    .net6有自带的logging组件,还有很多优秀的开源log组件,如NLog,serilog,这里我们使用serilog组件来构建日志模块,这篇文章主要介绍了如何在.Net6 web api中记录每次接口请求的日志,需要的朋友可以参考下
    2023-06-06
  • 解决asp.net core在输出中文时乱码的问题

    解决asp.net core在输出中文时乱码的问题

    最近在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题。那么这该如何解决呢?下面通过这篇文章来一起看看吧,文中给出了详细的解决方法,相信对大家有一定的参考价值。
    2016-12-12
  • ASP.NET操作MySql数据库的实例代码讲解

    ASP.NET操作MySql数据库的实例代码讲解

    这篇文章主要介绍了ASP.NET操作MySql数据库的实例代码讲解,需要的朋友可以参考下
    2016-12-12

最新评论