創建ASP.NET樹視圖我有一個要求,建立在ASP.Net一個多層次樹狀(用VB),但我完全被卡住就如何啓動這個。目前我的TreeView是一個固定的2級的做法,但現在我需要重寫這是更具活力和支持被添加到我們的數據庫表額外的水平。多層次
所以這個樹狀需要支持的多層次需要,而無需我們要添加新的臺階,每次重寫任何代碼,理想情況下,我們將只在數據庫級別插入數據。
我想我有數據庫部分設計正確,我創建了2個表Menu
和MenuItems
Menu
有2列ItemID
和ChildID
MenuItems
有2列ItemID
和Description
這樣做查詢:
SELECT
menu.Item_ID,
menu.Child_ID ,
parent.ID,
parent.Description,
child.ID,
child.Description
FROM
tblSupportTicketMenu menu
JOIN
tblSupportTicketMenuItems parent
ON
parent.ID = menu.Item_ID
JOIN
tblSupportTicketMenuItems child
ON
child.ID = menu.Child_ID
將返回以下數據:
Item_ID Child_ID ID Description ID Description
----------- ----------- ----------- ---------------------------------------------------------------------------------------------------- ----------- ----------------------------------------------------------------------------------------------------
32 33 32 Level 1 33 Level 2
33 34 33 Level 2 34 Level 3
35 36 35 Item 2 Level 1 36 Item 2 Level 2
36 37 36 Item 2 Level 2 37 Item 2 Level 3
從這裏我不能確定哪去了,我讀了asp的樹形目錄可以利用XML作爲它的數據源,這似乎是一個好主意,但我怎麼能選擇數據轉換爲支持多個級別的格式等?
如果有人知道如何做到這一點還是可以鏈接我的指導,我會很感激,如果也這樣做的XML是一個壞主意,我接受其他的建議,我還在學習ASP.Net所以我想正確地做到這一點。
爲了徹底,這是我目前正在替換的代碼,它爲我生成了treeview。
Dim ds As New DataTable
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())
Dim cmd As New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spGetMenuItemsForTickets"
cmd.Connection = conn
Using da As New SqlDataAdapter(cmd)
conn.Open()
da.Fill(ds)
conn.Close()
End Using
Dim ParentIds As List(Of Integer) = New List(Of Integer)
For Each row As DataRow In ds.Rows
If ParentIds.Contains(row("ParentID")) Then
'' Do Nothing
Else
ParentIds.Add(row("ParentID"))
End If
Next
For Each Parent As Integer In ParentIds
Dim parentNode As New System.Web.UI.WebControls.TreeNode
For Each child In ds.Rows
If (child("ParentID") = Parent) Then
Dim childNode As New System.Web.UI.WebControls.TreeNode
parentNode.Text = child("ParentDescription")
parentNode.Value = child("ParentID")
parentNode.Expanded = False
childNode.Text = child("ChildDescription")
childNode.Value = child("ChildID")
parentNode.SelectAction = TreeNodeSelectAction.None
parentNode.ChildNodes.Add(childNode)
End If
Next
trvItem.Nodes.Add(parentNode)
Next
trvItem.Nodes(0).Text += String.Empty