Recursive TreeView in ASP.NET

前端 未结 3 2026
梦如初夏
梦如初夏 2020-11-30 09:27

I have an object of type list from which I wish to use to populate a treeview in asp.net c#.

Each object item has:

id | Name | ParentId
3条回答
  •  渐次进展
    2020-11-30 09:56

    This is a sample with Category entity that references itself. First we should prepare our data source:

    public class Category
        {
            public int Id { get; set; }
            public string  Name { get; set; }
            public int? ParentId { get; set; }
            public virtual Category Parent { get; set; }
            public virtual ICollection Children { get; set; }
            public byte[] Image { get; set; }
        }
    
    public class Product
        {
            public int Id { get; set; }
            public string Code { get; set; }
            public string Name { get; set; }
            public Category ProductCategory { get; set; }
            public int ProductCategoryId { get; set; }
            public byte[] Image { get; set; }
        }
    
    public List GethierarchicalTree(int? parentId=null)
            {
                var allCats = new BaseRepository().GetAll();
    
                return allCats.Where(c => c.ParentId == parentId)
                                .Select(c => new Category()
                                {
                                    Id = c.Id,
                                    Name = c.Name,
                                    ParentId = c.ParentId,
                                    Children = GetChildren(allCats.ToList(), c.Id)
                                })
                                .ToList();
            }
    
            public List GetChildren(List cats, int parentId)
            {
                return cats.Where(c => c.ParentId == parentId)
                        .Select(c => new Category
                        {
                            Id = c.Id,
                            Name = c.Name,
                            ParentId = c.ParentId,
                            Children = GetChildren(cats, c.Id)
                        })
                        .ToList();
            }
    

    Then in our code behind we have:

     protected void Page_Load(object sender, EventArgs e)
            {
                var hierarchicalData = new CategoryRepository().GethierarchicalTree();
                tv1.Nodes.Clear();
                var root = new TreeNode("0","Root");
                tv1.Nodes.Add(root);
                BindTreeRecursive(hierarchicalData, root);
            }
    
            private void BindTreeRecursive(List hierarchicalData, TreeNode node)
            {
                foreach (Category category in hierarchicalData)
                {
                    if (category.Children.Any())
                    {
                        var n = new TreeNode(category.Name, category.Id.ToString());
                        node.ChildNodes.Add(n);
                        BindTreeRecursive(category.Children.ToList(), n);
                    }
                    else
                    {
                        var n = new TreeNode(category.Name, category.Id.ToString());
                        node.ChildNodes.Add(n);
    
                        if (new ProductRepository().Get(a => a.ProductCategoryId == category.Id).Any())
                        {
                            var catRelatedProducts = new ProductRepository().Get(a => a.ProductCategoryId == category.Id).ToList();
    
                            foreach (Product product in catRelatedProducts)
                            {
                                n.ChildNodes.Add(new TreeNode(product.Name,product.Id.ToString()));
                            }
                        }
                    }
                }
            }
    

提交回复
热议问题