efficient way binding nested repeater 3 levels deep

后端 未结 1 1484
旧巷少年郎
旧巷少年郎 2020-12-21 12:06

I have 3 levels deep repeters which bind to the following:

MainCategories - bind to top repeater

SubCategories - bind to repeater in the 2nd level

Su

相关标签:
1条回答
  • 2020-12-21 12:34

    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();
    
    0 讨论(0)
提交回复
热议问题