绑定嵌套转发器 3 层深度的有效方法
我有 3 个级别的深度中继器,它们绑定到以下内容:
MainCategories - 绑定到顶部中继器
SubCategories - 绑定到第二级中的中继器
SubSubCategories - 绑定到第三级中的中继器
到目前为止,我通过使用 itemdatabound 事件实现了数据绑定中继器并传递类别 ID 以过滤下面的级别(例如:获取 MainCategory 1 的所有子类别,获取所有MainCategory2 的子类别。 这当然会导致多次访问数据库,效率很低。
有没有办法只进行3次查询: 1. 获取所有主类别并绑定到顶级中继器, 2. 获取所有子类别并以某种方式绑定到二级中继器 3. 获取所有子子类别并绑定到第三级中继器。
在asp.net c# 中如何实现这一点?
I have 3 levels deep repeters which bind to the following:
MainCategories - bind to top repeater
SubCategories - bind to repeater in the 2nd level
SubSubCategories - bind to repeater in the 3rd level
Up to now, I acheived the databinding by using the itemdatabound event of the repeaters and passing the category id in order to filter the level beneath (e.g.: get all SubCategories for MainCategory 1, get all Subcategoris for MainCategory2).
This of course results in many trips to the database, and is inefficient.
Is there a way to make only 3 queries:
1. get all Main Categories and bind to top rpeater,
2. get all sub categories and bind somehow to 2nd level repeaters
3. get all subsub categories and bind to 3rd level repeaters.
How can this be acheived in asp.net c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,请按照以下步骤操作:
首先将数据放入 DataTable 中,例如
dataTableMainCategories
,然后从dataTableMainCategories 中过滤
SubCategories
和SubSubCategories
数据表。最后,在ItemDataBound
处编写以下代码块,并为SubCategories
和SubSubCategories
DataTable 添加所需的列,然后再导入筛选的行。将所有主类别填充到一个表中,并绑定到 MainCategory 重复器 (
rptrMainCategories
),并将所有子类别和子子类别填充到dataTableCategories
数据表中。更新
如果您使用类型化(自定义类型化)数据,则可以通过以下方式选择数据:
在事件
rptrMainCategories_ItemDataBound
处编写以下代码并绑定到转发器:在事件
>rptrSubCategories_ItemDataBound
编写以下代码并绑定到转发器:To do so, please follow below steps:
First of all data into a DataTable say
dataTableMainCategories
and then filterSubCategories
andSubSubCategories
fromdataTableMainCategories
data table. Finally, atItemDataBound
write below code block and forSubCategories
andSubSubCategories
DataTable add desired column before importing filtered rows.Populate all main categories into a table and Bind to MainCategory repeater (
rptrMainCategories
) and the populate all sub and sub sub categories intodataTableCategories
data table.Update
If you use typed(custom typed) data then you can choose data in below ways:
At the event
rptrMainCategories_ItemDataBound
write below code and bind to repeater:At the event
rptrSubCategories_ItemDataBound
write below code and bind to repeater: