ASP.NET 中的递归 TreeView

发布于 2024-08-28 07:29:37 字数 519 浏览 5 评论 0原文

我有一个列表类型的对象,我希望用它来填充 asp.net c# 中的树视图。

每个对象项都有:

id | Name | ParentId

例如:

id | Name     | ParentId
-------------------------
1  | Alice    | 0
2  | Bob      | 1
3  | Charlie  | 1
4  | David    | 2

在上面的示例中,父项是 Alice,有两个孩子 Bob 和 Charlie。大卫是鲍勃的孩子。

我在尝试在 c# ASP.NET 中递归动态填充树视图时遇到了很多问题

有人有一个简单的解决方案吗?

顺便说一句:您可以使用 People.Id、People.Name 和 People.ParentId 来访问成员,因为它是属于列表的对象。

到目前为止,我可以向您发布我的代码(进行了多次尝试),但不确定它有多大用处。

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

so for example:

id | Name     | ParentId
-------------------------
1  | Alice    | 0
2  | Bob      | 1
3  | Charlie  | 1
4  | David    | 2

In the above example, the parent would be Alice having two children Bob and Charlie. David is the child of Bob.

I have had many problems trying to dynamically populate the treeview recursively in c# ASP.NET

Does any one have a simple solution?

Btw: you can use People.Id, People.Name and People.ParentId to access the members since it is an object belonging to list.

I can post you my code so far (many attempts made) but not sure how useful it will be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美的太阳 2024-09-04 07:29:37

我认为这应该让你开始。我创建了一个 MyObject 类来模仿您的对象。

public class MyObject
{
    public int Id;
    public int ParentId;
    public string Name;
}

这是一种基于列表递归添加树视图节点的方法。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<MyObject> list = new List<MyObject>();
        list.Add(new MyObject(){Id=1, Name="Alice", ParentId=0});
        list.Add(new MyObject(){Id=2, Name="Bob", ParentId=1});
        list.Add(new MyObject(){Id=3, Name="Charlie", ParentId=1});
        list.Add(new MyObject(){Id=4, Name="David", ParentId=2});            

        BindTree(list, null);            
    }
}

private void BindTree(IEnumerable<MyObject> list, TreeNode parentNode)
{
    var nodes = list.Where(x => parentNode == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentNode.Value));
    foreach (var node in nodes)
    {
        TreeNode newNode = new TreeNode(node.Name, node.Id.ToString());
        if (parentNode == null)
        {
            treeView1.Nodes.Add(newNode);
        }
        else
        {
            parentNode.ChildNodes.Add(newNode);
        }
        BindTree(list, newNode);
    }
}

I think this should get you started. I created a MyObject class to mimic your object .

public class MyObject
{
    public int Id;
    public int ParentId;
    public string Name;
}

Here is a method to recursivley add tree view nodes based on the list.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<MyObject> list = new List<MyObject>();
        list.Add(new MyObject(){Id=1, Name="Alice", ParentId=0});
        list.Add(new MyObject(){Id=2, Name="Bob", ParentId=1});
        list.Add(new MyObject(){Id=3, Name="Charlie", ParentId=1});
        list.Add(new MyObject(){Id=4, Name="David", ParentId=2});            

        BindTree(list, null);            
    }
}

private void BindTree(IEnumerable<MyObject> list, TreeNode parentNode)
{
    var nodes = list.Where(x => parentNode == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentNode.Value));
    foreach (var node in nodes)
    {
        TreeNode newNode = new TreeNode(node.Name, node.Id.ToString());
        if (parentNode == null)
        {
            treeView1.Nodes.Add(newNode);
        }
        else
        {
            parentNode.ChildNodes.Add(newNode);
        }
        BindTree(list, newNode);
    }
}
你不是我要的菜∠ 2024-09-04 07:29:37

这是一个具有引用自身的类别实体的示例。首先我们应该准备我们的数据源:

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<Category> 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<Category> GethierarchicalTree(int? parentId=null)
        {
            var allCats = new BaseRepository<Category>().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<Category> GetChildren(List<Category> 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();
        }

然后在我们的代码中我们有:

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

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<Category> 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<Category> GethierarchicalTree(int? parentId=null)
        {
            var allCats = new BaseRepository<Category>().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<Category> GetChildren(List<Category> 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<Category> 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()));
                        }
                    }
                }
            }
        }
薄情伤 2024-09-04 07:29:37
    //In load for example
    if (!IsPostBack)
    {
            DataSet ds = new DataSet();
            ds = getRoles(); //function that gets data collection from db.

            tvRoles.Nodes.Clear();

            BindTree(ds, null); 
            tvRoles.DataBind();

    }       

    private void BindTree(DataSet ds, TreeNode parentNode)
    {
        DataRow[] ChildRows;
        if (parentNode == null)
        {
            string strExpr = "ParentId=0";
            ChildRows = ds.Tables[0].Select(strExpr);                    
        }
        else
        {
            string strExpr = "ParentId=" + parentNode.Value.ToString();
            ChildRows = ds.Tables[0].Select(strExpr); 
        }   
        foreach (DataRow dr in ChildRows)
        {
            TreeNode newNode = new TreeNode(dr["Name"].ToString(), dr["Id"].ToString());
            if (parentNode == null)
            {
                tvRoles.Nodes.Add(newNode);
            }
            else
            {
                parentNode.ChildNodes.Add(newNode);
            }
            BindTree(ds, newNode);
        }
    }
    //In load for example
    if (!IsPostBack)
    {
            DataSet ds = new DataSet();
            ds = getRoles(); //function that gets data collection from db.

            tvRoles.Nodes.Clear();

            BindTree(ds, null); 
            tvRoles.DataBind();

    }       

    private void BindTree(DataSet ds, TreeNode parentNode)
    {
        DataRow[] ChildRows;
        if (parentNode == null)
        {
            string strExpr = "ParentId=0";
            ChildRows = ds.Tables[0].Select(strExpr);                    
        }
        else
        {
            string strExpr = "ParentId=" + parentNode.Value.ToString();
            ChildRows = ds.Tables[0].Select(strExpr); 
        }   
        foreach (DataRow dr in ChildRows)
        {
            TreeNode newNode = new TreeNode(dr["Name"].ToString(), dr["Id"].ToString());
            if (parentNode == null)
            {
                tvRoles.Nodes.Add(newNode);
            }
            else
            {
                parentNode.ChildNodes.Add(newNode);
            }
            BindTree(ds, newNode);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文