Treeview没有正确添加为孩子

时间:2017-06-18 02:59:10

标签: c# asp.net .net vb.net treeview

我有以下代码来扩展树节点以将childeren添加到其父节点,直到第n级。

问题:孩子总是被添加到第一级,而应该被添加到第n级,直到它适当的父级。

以下代码:

public void Populate_Node(Object sender, TreeNodeEventArgs e)
{


        // Authenticating
        TreeProvider cmsTree = new TreeProvider(MembershipContext.AuthenticatedUser);
        //selecting the expanding node
        var node = cmsTree.SelectSingleNode(SiteContext.CurrentSiteName, "/Level1-2ndItem", "en-US");
        CMS.DocumentEngine.TreeNodeCollection myChildren = node.AllChildren;

        //Making array of child treenodes  of selected node
        IEnumerable<CMS.DocumentEngine.TreeNode> TreeNodes = myChildren.AsEnumerable<CMS.DocumentEngine.TreeNode>();
        foreach (var tree in myChildren)
        {
            System.Web.UI.WebControls.TreeNode ParenTreeNode = new System.Web.UI.WebControls.TreeNode();
            ParenTreeNode.Text = tree.NodeID.ToString() + ". " + SiteContext.CurrentSiteName;
            ParenTreeNode.Value = tree.NodeID.ToString();
            AddExpandedNodes(ParenTreeNode);

        }

}

private void AddExpandedNodes(System.Web.UI.WebControls.TreeNode TreeNode)
{  
    CMS.DocumentEngine.TreeProvider cmsTree = new CMS.DocumentEngine.TreeProvider(CMS.Membership.MembershipContext.AuthenticatedUser);
    var node = cmsTree.SelectSingleNode(CMS.SiteProvider.SiteContext.CurrentSiteName, "/", "en-US");
    CMS.DocumentEngine.TreeNodeCollection myChildren = node.AllChildren;
    IEnumerable<CMS.DocumentEngine.TreeNode> childTreeNodes = myChildren.AsEnumerable<CMS.DocumentEngine.TreeNode>().Where(x => x.NodeParentID == Convert.ToInt32(TreeNode.Value));
    foreach (var childTree in childTreeNodes)
    {
        System.Web.UI.WebControls.TreeNode ChildNode = new System.Web.UI.WebControls.TreeNode();
        ChildNode.Text = childTree.NodeID.ToString() + ". " + childTree.DocumentName.ToString();
        ChildNode.Value = childTree.NodeID.ToString();      
        ChildNode.ChildNodes.Add(ChildNode);

        AddExpandedNodes(ChildNode);
        tvContentTree.Nodes.Add(ChildNode);
    }      
}

0 个答案:

没有答案