2017-05-10 71 views
0

我必須動態生成樹視圖,我在哪裏找到要在第n級生成的問題。TreeView生成樹問題

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

     for (int i = 0; i < 3; i++) 
     { 
      foreach (var tree in treeNodes.Where(x => x.NodeLevel == i)) 
      { 
       System.Web.UI.WebControls.TreeNode ParentNode = new System.Web.UI.WebControls.TreeNode(); 
       ParentNode.Text = "Document ID " + tree.DocumentID.ToString() + ". " + tree.DocumentName.ToString() + " Level " + tree.NodeLevel + " Parent " + tree.NodeParentID + " Node ID " + tree.NodeID; 
       ParentNode.Value = tree.DocumentID.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> treeNodes = myChildren.AsEnumerable<TreeNode>();    

     foreach (var tree in treeNodes.Where(x => x.NodeParentID == Convert.ToInt32(tNode.Value))) 
     { 
      System.Web.UI.WebControls.TreeNode ChildNode = new System.Web.UI.WebControls.TreeNode(); 
      ChildNode.Text = "Document ID " + tree.DocumentID.ToString() + ". " + tree.DocumentName.ToString() + " Level " + tree.NodeLevel + " Parent " + tree.NodeParentID + " Node ID " + tree.NodeID; 
      ChildNode.Value = tree.DocumentID.ToString(); 
      tNode.ChildNodes.Add(ChildNode); 
     } 
} 

如何我們可以重寫代碼來生成對第n級樹:

我可以使用下面的代碼生成在2級的樹?

回答

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