用于生成树的TreeView问题

时间:2017-05-10 13:58:26

标签: c# asp.net .net treeview

我必须动态生成树视图,我发现要在第n级生成问题。

我可以使用以下代码生成2个级别的树:

B42<T>

我们如何重写代码以生成第n级树?

1 个答案:

答案 0 :(得分:0)

我自己解决了

    protected void Page_Load(object sender, EventArgs e)
    {
        TreeProvider cmsTree = new TreeProvider(MembershipContext.AuthenticatedUser);
        var node = cmsTree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-US");

            TreeNodeCollection myChildren = node.AllChildren;
            IEnumerable<TreeNode> treeNodes = myChildren.AsEnumerable<TreeNode>();
            foreach (var tree in treeNodes.Where(x => x.NodeLevel ==0))
            {
                System.Web.UI.WebControls.TreeNode ParentNode = new System.Web.UI.WebControls.TreeNode();
                ParentNode.Text = "Root";
                ParentNode.Value = tree.NodeID.ToString();
                AddNodes(ParentNode);
                tvContentTree.Nodes.Add(ParentNode);
            }           

    }
    /// <summary>
    /// Adding child for the parent 
    /// </summary>
    /// <param name="tNode"></param>
    private void AddNodes(System.Web.UI.WebControls.TreeNode tNode)
    {
        TreeProvider cmsTree = new TreeProvider(MembershipContext.AuthenticatedUser);
        var node = cmsTree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-US");

        TreeNodeCollection myChildren = node.AllChildren;
        IEnumerable<TreeNode> childTreeNodes = myChildren.AsEnumerable<TreeNode>();

        foreach (var childTree in childTreeNodes.Where(x => x.NodeParentID == Convert.ToInt32(tNode.Value)))
        {
            System.Web.UI.WebControls.TreeNode ChildNode = new System.Web.UI.WebControls.TreeNode();
            ChildNode.Text = childTree.DocumentName.ToString();
            ChildNode.Value = childTree.NodeID.ToString();
            tNode.ChildNodes.Add(ChildNode);                
            AddNodes(ChildNode);
        }
    }