views:

121

answers:

0

Hi, What I'm want to accomplish is to build a hierarchical menu build on my custom sql sitemapprovider. Now when I just write down the structure using recursion it builds up correctly like this:

Main
-- Item1
-- Item2
Second
-- Item1
-- Item2
---- Sub1
---- Sub2
Third

But what I would like to accomplish is that the childeren only show when I'm on the requested page. Lik this:

When I click on Main and go to the main page the structure must render like this
Main
-- Item1
-- Item2
Second
Third

When I click on Item(of Main) and go to that page the structure renders like this
Main
-- Item
---- Sub1
---- Sub2
-- Item2
Second
Third

As you can see the children are only shown when navigated to the request page. Like expanding a client tree navigation, but then server-side. (Like menu on left side) I'm using VB.NET and a recursive function to render the structure The code I'm currently using is this, but this just builds up the hierarchical structure of an unordered list and shows the child items of each node at the bottom of the list. Maybe someone knows how I could build this incremental menu so it shows the correct parent child relations when requesting a page.

Private _map As MySiteMapProvider = _
DirectCast(SiteMap.Providers("CustomSitemap"), MySiteMapProvider)

Private Function RenderNodes() As String
    Dim currentNode As SiteMapNode = _map.CurrentNode
    Dim rootNode As SiteMapNode = _map.RootNode
    Dim nodeBuilder As New StringBuilder
    Dim nodeStack As Stack(Of SiteMapNode) = New Stack(Of SiteMapNode)

    'Find Parent nodes of current node
    While Not currentNode.Equals(rootNode)
        nodeStack.Push(currentNode)
        currentNode = currentNode.ParentNode
    End While

    'Iterate through stack and build child nodes recursive
    For Each node As SiteMapNode In nodeStack
        With nodeBuilder
            .Append(RenderChildNodes(node, node.ChildNodes))
        End With
    Next
    Return nodeBuilder.ToString
End Function
Private Function RenderChildNodes(ByVal parent As SiteMapNode, ByVal coll As SiteMapNodeCollection) As String
    Dim mapBuilder As New StringBuilder
    For Each node As SiteMapNode In coll
        mapBuilder.AppendLine("<li>")
        mapBuilder.AppendFormat("<a href=""{0}"">{1}</a>", node.Url, node.Title)
        If node.HasChildNodes _
        And _map.CurrentNode.IsDescendantOf(parent) Then
            mapBuilder.AppendFormat("<ul>{0}</ul>", RenderChildNodes(node, node.ChildNodes))
        End If
        mapBuilder.AppendLine("</li>")
    Next
    Return mapBuilder.ToString
End Function