创建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