怎么在Repeater中嵌套Repeater

发布时间:2021-04-06 16:40:47 作者:Leah
来源:亿速云 阅读:152

本篇文章为大家展示了怎么在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>

上述内容就是怎么在Repeater中嵌套Repeater,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 演示asp.net中使用Repeater嵌套相关代码
  2. Repeater中嵌套使用Repeater的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

repeater

上一篇:怎么在PHP中对session_id进行修改

下一篇:怎么在css中调用服务器端字体

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》