绑定嵌套转发器 3 层深度的有效方法

发布于 2024-12-20 15:13:03 字数 399 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-12-27 15:13:03

为此,请按照以下步骤操作:

首先将数据放入 DataTable 中,例如 dataTableMainCategories,然后从 dataTableMainCategories 中过滤 SubCategoriesSubSubCategories数据表。最后,在 ItemDataBound 处编写以下代码块,并为 SubCategoriesSubSubCategories DataTable 添加所需的列,然后再导入筛选的行。

将所有主类别填充到一个表中,并绑定到 MainCategory 重复器 (rptrMainCategories),并将所有子类别和子子类别填充到 dataTableCategories 数据表中。

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();
    }
}

更新

如果您使用类型化(自定义类型化)数据,则可以通过以下方式选择数据:

//Populate all main categories
public List<Category>  MainCategories { get; set; }

//Populate all sub and sub categories
public List<Category> SubCategories { get; set; }

在事件 rptrMainCategories_ItemDataBound 处编写以下代码并绑定到转发器:

List<Category> subCategory = SubCategories.Where(c => c.SubCategoryId = yourSubCategoryID);
rptrSubCategories.DataSource = subCategory ;
rptrSubCategories.DataBind();

在事件 >rptrSubCategories_ItemDataBound 编写以下代码并绑定到转发器:

List<Category> subSubCategory = SubCategories.Where(c => c.SubSubCategoryId = yourSubSubCategoryID);
rptrSubSubCategory.DataSource = subSubCategory ;
rptrSubSubCategory.DataBind();

To do so, please follow below steps:

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.

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();
    }
}

Update

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; }

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文