ASP.NET 中的递归 TreeView
我有一个列表类型的对象,我希望用它来填充 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这应该让你开始。我创建了一个
MyObject
类来模仿您的对象。这是一种基于列表递归添加树视图节点的方法。
I think this should get you started. I created a
MyObject
class to mimic your object .Here is a method to recursivley add tree view nodes based on the list.
这是一个具有引用自身的类别实体的示例。首先我们应该准备我们的数据源:
然后在我们的代码中我们有:
This is a sample with Category entity that references itself. First we should prepare our data source:
Then in our code behind we have: