efficient way binding nested repeater 3 levels dee

2019-01-28 21:31发布

问题:

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#?

回答1:

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