有效的方式绑定嵌套直放站3级深 [英] efficient way binding nested repeater 3 levels deep
问题描述
我有3个级别可结合以下深repeters:
I have 3 levels deep repeters which bind to the following:
MainCategories - 绑定到上部中继
MainCategories - bind to top repeater
子类别 - 绑定到中继器中的第二级
SubCategories - bind to repeater in the 2nd level
SubSubCategories - 绑定到中继器中的第三级
SubSubCategories - bind to repeater in the 3rd level
到现在为止,我通过使用中继器的ItemDataBound事件,并通过类别ID以过滤下的水平acheived数据绑定(例如:获得所有子类别MainCategory 1,得到MainCategory2所有Subcategoris)。
这多次到数据库课程成绩和效率不高。
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.
有没有一种方法,使只有3个疑问:
1.获得所有主要类别并绑定到顶部rpeater,
2.让所有的子类别并以某种方式结合到第二层中继器
3.让所有subsub类别和绑定到3级中继器。
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.
如何解决这个在asp.net C#来acheived?
How can this be acheived in asp.net c#?
推荐答案
为此,请按照下列步骤操作:
To do so, please follow below steps:
所有数据首先进入一个DataTable发言权 dataTableMainCategories
,然后筛选子
和 SubSubCategories
从 dataTableMainCategories
数据表。最后,在的ItemDataBound
下面写code块和子
和 SubSubCategories
DataTable中添加所需的列导入过滤行之前。
First of all data into a DataTable say dataTableMainCategories
and then filter SubCategories
and SubSubCategories
from dataTableMainCategories
data table. Finally, at ItemDataBound
write below code block and for SubCategories
and SubSubCategories
DataTable add desired column before importing filtered rows.
填写所有主要类别表中并绑定到MainCategory中继器( rptrMainCategories
)和填充所有子和子子类别为 dataTableCategories
数据表。
Populate all main categories into a table and Bind to MainCategory repeater (rptrMainCategories
) and the populate all sub and sub sub categories into dataTableCategories
data table.
protected void rptrMainCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType== ListItemType.Item)
{
int subCategoryID = 5; // Pass your subcategory id to be filtered
Repeater rptrSubCategories = (Repeater)e.Item.FindControl("rptrSubCategories");
DataTable dtSubCategory = new DataTable();
dtSubCategory.Columns.Add(); // Add your columns as SubCatagory
DataRow[] dataRows = dataTableCategories.Select("SubCategoryID= " + subCategoryID + "");
foreach (DataRow dataRow in dataRows)
{
dtSubCategory.ImportRow(dataRow);
}
rptrSubCategories.DataSource = dtSubCategory;
rptrSubCategories.DataBind();
}
}
protected void rptrSubCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType== ListItemType.Item)
{
int subSubCategoryID = 55; // Pass your subsubcategory id to be filtered
Repeater rptrSubSubCategory = (Repeater)e.Item.FindControl("rptrSubSubCategory");
DataTable dtSubSubCategory = new DataTable();
dtSubSubCategory.Columns.Add(); // Add your columns as SubCatagory
DataRow[] dataRows = dataTableCategories.Select("SubSubCategoryID= " + subSubCategoryID + "");
foreach (DataRow dataRow in dataRows)
{
dtSubSubCategory.ImportRow(dataRow);
}
rptrSubSubCategory.DataSource = dtSubSubCategory;
rptrSubSubCategory.DataBind();
}
}
更新
如果您使用类型(自定义类型)的数据,那么你可以选择以下方式处理数据:
If you use typed(custom typed) data then you can choose data in below ways:
//Populate all main categories
public List<Category> MainCategories { get; set; }
//Populate all sub and sub categories
public List<Category> SubCategories { get; set; }
在活动 rptrMainCategories_ItemDataBound
下面写code和绑定到中继器:
At the event rptrMainCategories_ItemDataBound
write below code and bind to repeater:
List<Category> subCategory = SubCategories.Where(c => c.SubCategoryId = yourSubCategoryID);
rptrSubCategories.DataSource = subCategory ;
rptrSubCategories.DataBind();
在活动 rptrSubCategories_ItemDataBound
下面写code和绑定到中继器:
At the event rptrSubCategories_ItemDataBound
write below code and bind to repeater:
List<Category> subSubCategory = SubCategories.Where(c => c.SubSubCategoryId = yourSubSubCategoryID);
rptrSubSubCategory.DataSource = subSubCategory ;
rptrSubSubCategory.DataBind();
这篇关于有效的方式绑定嵌套直放站3级深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!