Asp.net 如何构建无限树层次结构?
我从昨天开始就一直在做这个,我完全被难倒了。我在VB.NET中工作,但如果你愿意这样回答,我可以理解C 基本上,我有一个SQL数据库中的项目,带有ID和父ID,我需要将它们放在一个树中,如下所示:Asp.net 如何构建无限树层次结构?,asp.net,vb.net,treeview,hierarchical-data,Asp.net,Vb.net,Treeview,Hierarchical Data,我从昨天开始就一直在做这个,我完全被难倒了。我在VB.NET中工作,但如果你愿意这样回答,我可以理解C 基本上,我有一个SQL数据库中的项目,带有ID和父ID,我需要将它们放在一个树中,如下所示: <ul> <li>Some item <ul> <li>Another item <ul> <li>This
<ul>
<li>Some item
<ul>
<li>Another item
<ul>
<li>This could go forever
<ul>
<li>Still going</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
我理解为什么它不能工作,因为它没有一个模板来继续树。所以我在想办法解决这个问题
我考虑过编写一个函数,只需在代码中构建字符串,并将其粘贴到带有asp标记的html中。在从数据库中取出数据时,我不确定如何执行此操作。我找到了一个临时解决方案,尽管它效率极低。万一有人偶然发现我的问题,这至少会让你通过。对于大量数据,加载可能需要整整一分钟 如果有人能对如何提高效率发表评论,那就太好了 代码隐藏:
Private Dim dt As New DataTable
Public Function BuildTree(ByVal ID As String) As String
Dim sb As New StringBuilder
dt = YourDatabase.GetChildren(ID) '<-- You'll have to write this function
sb.AppendLine("")
If dt.Rows.Count > 0 Then
sb.AppendLine("<ul>")
If dt.Rows.Count > 1 Then
For Each row As DataRow In dt.Rows
sb.AppendLine("<li>" & row("ItemName"))
' Recursive call
sb.AppendLine(BuildTree(row("ID").ToString))
sb.AppendLine("</li>")
Next
Else
sb.AppendLine("<li>" & dt.Rows(0)("ItemName").ToString & "</li>")
End If
sb.AppendLine("</ul>")
End If
Return sb.ToString
End Function
Private Dim dt作为新数据表
公共函数BuildTree(ByVal ID作为字符串)作为字符串
使某人成为新的架线工
dt=YourDatabase.GetChildren(ID)'
ds.Relations.Add("relation",
ds.Tables("Items").Columns("ID"),
ds.Tables("Items").Columns("ParentID"),
False)
Private Dim dt As New DataTable
Public Function BuildTree(ByVal ID As String) As String
Dim sb As New StringBuilder
dt = YourDatabase.GetChildren(ID) '<-- You'll have to write this function
sb.AppendLine("")
If dt.Rows.Count > 0 Then
sb.AppendLine("<ul>")
If dt.Rows.Count > 1 Then
For Each row As DataRow In dt.Rows
sb.AppendLine("<li>" & row("ItemName"))
' Recursive call
sb.AppendLine(BuildTree(row("ID").ToString))
sb.AppendLine("</li>")
Next
Else
sb.AppendLine("<li>" & dt.Rows(0)("ItemName").ToString & "</li>")
End If
sb.AppendLine("</ul>")
End If
Return sb.ToString
End Function
<%#BuildTree(Container.DataItem("ID").ToString)%>