【问题标题】:Creating an ASP.NET Treeview with multiple levels创建具有多个级别的 ASP.NET 树视图
【发布时间】:2012-04-04 21:58:00
【问题描述】:

我需要在 ASP.Net(使用 VB)中创建多级树视图,但我完全不知道如何启动它。目前我的树视图是一个固定的 2 级方法,但现在我需要重写它以使其更加动态并支持将额外的级别添加到我们的数据库表中。

所以这个树视图需要支持尽可能多的级别,而不必每次我们想要添加新级别时都重写任何代码,理想情况下我们只需在数据库级别插入数据。

我认为我的数据库部分设计正确,我创建了 2 个表 MenuMenuItems

Menu 有 2 列 ItemIDChildID

MenuItems 有 2 列 ItemIDDescription

执行此查询:

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 Treeview 可以将 XML 作为其数据源,这似乎是个好主意,但是我如何将数据选择为支持多个级别等的格式?

如果有人知道如何执行此操作或可以将我链接到指南,我将不胜感激,而且如果将其作为 XML 执行是一个坏主意,我愿意接受其他建议,我仍在学习 ASP.Net,所以我想要正确地做到这一点。

为了彻底,这是我当前正在替换的代码,它为我生成树视图。

   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

【问题讨论】:

    标签: asp.net sql xml vb.net treeview


    【解决方案1】:

    您创建的数据库结构似乎没问题,但是将 itemid 重命名为 parentid 并将 childid 重命名为 itemid 对我来说更容易理解(我希望看到当前项目的 parentid)

    您可以通过阅读以下链接逐步进行,他们试图使其简单易懂。我希望这会有所帮助。

    http://aspalliance.com/732_Display_Hierarchical_Data_with_TreeView_in_ASPNET_20

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2015-08-26
      • 2019-06-05
      • 1970-01-01
      相关资源
      最近更新 更多