输出此数据类的数据

时间:2010-06-23 12:39:12

标签: asp.net menu loops nested datalist

有人可以帮助使用哪些控件/方法将数据从这个类中提取到嵌套列表中,即父,子,孙,伟大的孩子?

当我跑步时:

Dim menu As New MenuBuilder
        Response.Write(menu.BuildMenu().ToString)

我只获得顶级链接而没有子级别

感谢。

Public Class MenuBuilder
    Private _rawData As List(Of MenuData) = MenuDataImport.GetMenuData()

    Public Function BuildMenu() As String

        'create a new list with the top level0 items only
        Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))

        'sort the top level items
        builtMenu.Sort(Function(a, b) a.OrderID - b.OrderID)

        'get the children items
        For Each menuData As MenuData In builtMenu
            If menuData.Children IsNot Nothing Then
                menuData.Children.AddRange(GetChildren(menuData))
            End If
        Next
        Dim Builder = New StringBuilder()

        'builtMenu contains the right items with the right children in order.
        For Each menuData As MenuData In builtMenu
            If menuData.Url IsNot Nothing And menuData.Moduleid = 0 Then
                'format as a standard link
                Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
            ElseIf InStr(menuData.Url, "file:///") Then
                'format as a file link
                Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
            Else
                'format as a querystring id link
                Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", menuData.Id, menuData.Heading)
            End If
            If menuData.Children IsNot Nothing Then
                For Each child As MenuData In menuData.Children
                    If child.Url IsNot Nothing And child.Moduleid = 0 Then
                        'format as a standard link
                        Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
                    ElseIf InStr(child.Url, "file:///") Then
                        'format as a file link
                        Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
                    Else
                        'format as a querystring id link
                        Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", child.Id, child.Heading)
                    End If

                    For Each grandChild As MenuData In child.Children
                        If grandChild.Url IsNot Nothing And grandChild.Moduleid = 0 Then
                            'format as a standard link
                            Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
                        ElseIf InStr(grandChild.Url, "file:///") Then
                            'format as a file link
                            Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
                        Else
                            'format as a querystring id link
                            Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", grandChild.Id, grandChild.Heading)
                        End If

                        For Each greatgrandChild As MenuData In grandChild.Children
                            If greatgrandChild.Url IsNot Nothing And greatgrandChild.Moduleid = 0 Then
                                'format as a standard link
                                Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
                            ElseIf InStr(grandChild.Url, "file:///") Then
                                'format as a file link
                                Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
                            Else
                                'format as a querystring id link
                                Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", greatgrandChild.Id, greatgrandChild.Heading)
                            End If
                        Next
                    Next

                Next

            End If

        Next
        Return Builder.ToString()
    End Function

    Private Function GetChildren(ByVal parent As MenuData) As List(Of MenuData)
        Dim children As List(Of MenuData) = _rawData.FindAll(Function(item) item.GroupingID = parent.Id)

        'sort the menu items
        children.Sort(Function(a, b) a.OrderID - b.OrderID)

        'get children's children
        For Each menuData As MenuData In children
            If menuData.Children IsNot Nothing Then
                menuData.Children.AddRange(GetChildren(menuData))
            End If
        Next
        Return children
    End Function
End Class

1 个答案:

答案 0 :(得分:0)

公共类MenuBuilder     私有_rawData作为List(Of MenuData)= MenuDataImport.GetMenuData()

Public Function BuildMenu() As String

    'create a new list with the top level0 items only
    Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))

    'sort the top level items
    builtMenu.Sort(Function(a, b) a.OrderID - b.OrderID)

    'get the children items
    For Each menuData As MenuData In builtMenu
        If menuData.Children IsNot Nothing Then
            menuData.Children.AddRange(GetChildren(menuData))
        End If
    Next
    Dim Builder = New StringBuilder()

    Builder.Append("<ul>")
    'builtMenu contains the right items with the right children in order.
    For Each menuData As MenuData In builtMenu
        Builder.Append("<li>")
        If menuData.Url IsNot Nothing And menuData.Moduleid = 0 Then
            'format as a standard link
            Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
        ElseIf InStr(menuData.Url, "file:///") Then
            'format as a file link
            Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
        Else
            'format as a querystring id link
            Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", menuData.Id, menuData.Heading)
        End If
        If menuData.Children IsNot Nothing Then
            Builder.Append("<ul>")
            For Each child As MenuData In menuData.Children
                Builder.Append("<li>")
                If child.Url IsNot Nothing And child.Moduleid = 0 Then
                    'format as a standard link
                    Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
                ElseIf InStr(child.Url, "file:///") Then
                    'format as a file link
                    Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
                Else
                    'format as a querystring id link
                    Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", child.Id, child.Heading)
                End If

                If child.Children IsNot Nothing Then
                    Builder.Append("<ul>")
                    For Each grandChild As MenuData In child.Children
                        Builder.Append("<li>")
                        If grandChild.Url IsNot Nothing And grandChild.Moduleid = 0 Then
                            'format as a standard link
                            Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
                        ElseIf InStr(grandChild.Url, "file:///") Then
                            'format as a file link
                            Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
                        Else
                            'format as a querystring id link
                            Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", grandChild.Id, grandChild.Heading)
                        End If

                        If grandChild.Children IsNot Nothing Then
                            Builder.Append("<ul>")
                            For Each greatgrandChild As MenuData In grandChild.Children
                                Builder.Append("<li>")
                                If greatgrandChild.Url IsNot Nothing And greatgrandChild.Moduleid = 0 Then
                                    'format as a standard link
                                    Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
                                ElseIf InStr(grandChild.Url, "file:///") Then
                                    'format as a file link
                                    Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
                                Else
                                    'format as a querystring id link
                                    Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", greatgrandChild.Id, greatgrandChild.Heading)
                                End If
                                Builder.Append("</li>")
                            Next
                            Builder.Append("</ul>")
                        End If
                        Builder.Append("</li>")
                    Next
                    Builder.Append("</ul>")
                End If
                Builder.Append("</li>")
            Next
            Builder.Append("</ul>")
        End If
        Builder.Append("</li>")
    Next
    Builder.Append("</ul>")
    Return Builder.ToString()
End Function