这样的数据在数据库的表里面肯定能知道某个节点的父亲是谁的,根节点获取到以后,就根据根节点的ID获取所有的子节点,循环所有子节点,在给子节点的子节点赋值的时候就递归调用,这样就可以拿出你想要的结构了。下面是代码
public DepartmentTreeNode GetRootNode()
{
System.Web.UI.WebControls.TreeNode node = RootNode();
DepartmentTreeNode rootnode = new DepartmentTreeNode(node.Value, node.Text);
rootnode.IsExpend = true;
rootnode.Selected = true;
foreach (System.Web.UI.WebControls.TreeNode n in node.ChildNodes)
{
DepartmentTreeNode child = new DepartmentTreeNode(n.Value,n.Text);
child.IsExpend = false;
child.ChildNodes = GetNodeList(child.Value);
child.HasChildNodes = ChildNodes.Count > 0 ? true : false;
rootnode.ChildNodes.Add(child);
}
return rootnode;
}
private IList<DepartmentTreeNode> GetNodeList(string id)
{
IList<DepartmentTreeNode> list = new List<DepartmentTreeNode>();
foreach (System.Web.UI.WebControls.TreeNode node in GetChilds(id))
{
DepartmentTreeNode depnode = new DepartmentTreeNode(node.Value, node.Text);
depnode.ChildNodes = GetNodeList(node.Value);
list.Add(depnode);
}
return list;
}
谢谢哈 问题已经解决 欢迎多多交流哈
欢迎有这方面经验的朋友给点思路