ASP.Net(VBで)で複数レベルのツリービューを作成する必要がありますが、これを開始する方法は完全に固まっています。現在のところ、私のtreeviewは固定2レベルのアプローチですが、今私はこれをより動的に書き直し、余分なレベルをデータベーステーブルに追加する必要があります。複数のレベルでASP.NETツリービューを作成する
このツリービューは、新しいレベルを追加するたびにコードを書き直すことなく、必要なだけ多くのレベルをサポートする必要があります。理想的にはデータベースレベルでデータを挿入します。
私は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としてこれを行うのは悪い考えです。私は他の提案にもオープンしています。私はこれを適切にしたいと思います。
これは私が現在ツリービューを生成している現在置き換え中のコードです。
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