最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

创建具有多个级别的 ASP.NET Treeview

SEO心得admin99浏览0评论
本文介绍了创建具有多个级别的 ASP.NET Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a requirement to create a multi level treeview in ASP.Net (with VB) but I am completely stuck on how to start this. Currently my treeview is a fixed 2 level approach but now I need to rewrite this to be more dynamic and support extra levels being added into our database tables.

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

So this treeview needs to support as many levels as needed without having to rewrite any code each time we want to add new level, Ideally we will just insert the data at the database level.

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

I think I have the database part designed correctly, I created 2 tables Menu and MenuItems

Menu 有 2 列 ItemID 和 ChildID

MenuItems 有 2 列 ItemID 和 Description

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

From here I am unsure where to go, I read that the asp Treeview can take XML as its datasource and this seems to be a good idea, but how could I select the data into a format which would support multiple levels etc?

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

If anyone knows how to do this or could link me to a guide I would be very appreciative, also if doing this as XML is a bad idea I am open to other suggestions, I'm still learning ASP.Net so I would like to do this properly.

要彻底,这是我目前正在替换的代码,它为我生成树视图.

To be thorough this is the code I am currently replacing which generates the treeview for me.

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

推荐答案

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

The database structure you have created seems ok however rename itemid to parentid and childid to itemid would be more understandable to me ( I like to see parentid for the current item)

您可以通过阅读以下链接逐步进行,他们试图使其易于理解.我希望这会有所帮助.

You can go step by step by reading following link , they tried to make it simple to understand. I hope this will help.

aspalliance/732_Display_Hierarchical_Data_with_TreeView_in_ASPNET_20

发布评论

评论列表(0)

  1. 暂无评论