2009-07-14 104 views
1

我一直在使用它來自一個DB以下數據建立一個樹:建立一個TreeView動態

ID   Name  ManagerID 
180002  john  180001 
180003  Michel 180002 
180005  smith 180003 

john 
|_Michel 
    |_ smith 

具體來說,我需要做的ASP.NET TreeView控件。 TreeView的深度不固定。

回答

0

您沒有指定是否使用C#或VB,但方法是相同的。

您首先爲每個頂級個人(沒有ManagerID的人或具有指示頂級的特殊ManagerID代碼)創建一個TreeNode。

然後,對於每個向當前經理報告的個人遞歸地創建一個新的TreeNode,並將其添加爲當前經理的子節點。找到向他彙報的每個人,並將這個功能應用於他們。當沒有人向當前人員報告時停止。

因此,假設你有這個表的細節數據集,你會做這樣的事情(VB爲例):

Sub PopulateTreeNode(
    CurrentNode As TreeNode, 
    CurrentManagerID As String, 
    Table As DataTable 
) As TreeNode 
    For Each Row As DataRow In _ 
    DataTable.Select("ManagerID = '" & CurrentManagerID & "'") 
    Dim CurrentUser As String = Row("ID").ToString() 
    Dim Node As New TreeNode(CurrentUser) 
    PopulateTreeNode(Node, CurrentUser, Table) 
    CurrentNode.Nodes.Add(Node) 
    Next 
End Sub 
+0

感謝它的工作原理是有任何避免回發,使節點被選中,但不會回發到服務器 – muthu 2009-07-14 12:35:30

+0

您可以將它包裝在UpdatePanel中,或者您可以設置樹的AutoPostBack屬性及其節點爲false。 – Welbog 2009-07-14 12:37:35