c# - 绑定(bind)嵌套中继器的有效方式 3 层深

标签 c# asp.net repeater

我有 3 个级别的深度重复器,它们绑定(bind)到以下内容:

MainCategories - 绑定(bind)到顶部转发器

子类别 - 绑定(bind)到二级转发器

SubSubCategories - 绑定(bind)到第三层的转发器

到目前为止,我通过使用中继器的 itemdatabound 事件并传递类别 ID 来实现数据绑定(bind)以过滤下面的级别(例如:获取 MainCategory 1 的所有子类别,获取 MainCategory2 的所有子类别)。 这当然会导致多次访问数据库,并且效率低下。

有没有办法只进行 3 个查询: 1.获取所有主要类别并绑定(bind)到top rpeater, 2.获取所有子类别并以某种方式绑定(bind)到二级中继器 3. 获取所有子子类别并绑定(bind)到第 3 级中继器。

如何在 asp.net c# 中实现这一点?

最佳答案

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

首先将所有数据放入DataTable 说dataTableMainCategories,然后从dataTableMainCategories 数据表中过滤SubCategoriesSubSubCategories .最后,在 ItemDataBound 代码块下方写入,并为 SubCategoriesSubSubCategories DataTable 在导入筛选行之前添加所需的列。

将所有主要类别填充到一个表中并绑定(bind)到 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 中编写以下代码并绑定(bind)到转发器:

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

在事件 rptrSubCategories_ItemDataBound 中编写以下代码并绑定(bind)到转发器:

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

关于c# - 绑定(bind)嵌套中继器的有效方式 3 层深,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8454129/

相关文章:

c# - 你能通过windows连接到Linux上的MySQL数据库吗

c# - LINUX 到 Windows 错误的编码响应

javascript - 如何让我的范围 slider 在 asp.net mvc 3 上的触摸屏上工作

c# - 阅读中继器内的文本框

c# - 如何在 Repeater 中获取文字内容值

c# - 你能触发具有自动属性的事件吗?

c# - 应用程序的类结构和数据库结构

html - 如何更改表格中 HTML 元素的位置

c# - 如何更改网页的背景颜色

qt - 如何在 QML 中移动/动画由中继器创建的组件?