Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

c# - efficient way binding nested repeater 3 levels deep

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...