2011-07-16 28 views
0

當我此刻的用戶填充一個TreeView控制的節點擴展父節點,以獲得TreeNode.Parent和TreeNode.ChildNodes - 第一次 - 使用填充按需爲描述於ASP.NET: How to Create an Expandable Empty TreeNode。人口過程正常。ASP.NET的TreeView:無法使用TreeView.TreeNodePopulate

問題是,當我試圖讓TreeNode.Parent我得到一個即使TreeNode不是一個根節點。此外,TreeNode.ChildNodes返回一個空集合,即使有指定的某些子節點TreeNode ...

有沒有對此的解釋?而且,我能做些什麼來解決它?

謝謝。

+0

代碼?錯誤消息的確切文本?它發生的線? –

+0

@Steve Wellens:你有沒有讀過這個問題?我沒有收到任何錯誤消息。此外,您希望看到哪部分代碼? –

回答

0

如果用戶選擇了根父節點,該代碼示例下面應該努力來填充使用數據的集合樹:

保護無效ParseTreeNodes(對象發件人,System.Web.UI.WebControls.TreeNodeEventArgs E)

{ 
    //Uncomment this line below and inspect the 'nodes' variable if you need to inspect all of the nodes 
    //var nodes = this.MyTreeView.Nodes; 

    //Some call to get data for example 
    var dataCollection = GetSomeDataForTheTree() 

    //Iterate through and create a new node for each row in the query results. 
    //Notice that the query results are stored in the table of the DataSet. 
    foreach (MyClassWithData data in dataCollection) 
    { 
     //Create the new node using the values from the collection: 
     TreeNode newNode = new TreeNode(data.LastName, system.ID) 
      { 
       //Set the PopulateOnDemand property to false, because these are leaf nodes 
       and do not need to be populated on subsequent calls. 
       PopulateOnDemand = false, 
       SelectAction = TreeNodeSelectAction.Select 
      }; 

     //Add the new node to the ChildNodes collection of the parent node.      
     e.node.ChildNodes.Add(NewNode); 
    } 
} 

我添加了額外的代碼行來檢查當前節點的樹狀視圖,如果您有與父節點的問題或知道Nodes集合中的具體數值。