How can I turn the output of asp:SiteMapPath into a list?

前端 未结 5 2354
借酒劲吻你
借酒劲吻你 2021-02-20 04:52

I\'m extremely unfamiliar with both .NET and VB.NET and can\'t quite figure out how to do this. Say I have code like this:

&
5条回答
  •  醉话见心
    2021-02-20 05:10

    You may have solved this by now but you could use this function to loop through all the items in the rootnode of a sitemap and their descendants and build up a nested list.

    You can remove If item.HasChildNodes Then sb.Append(ListChildNodes(item)) if you are only interested in the top level

     Public Function SiteMap() As String
            Return ListChildNodes(System.Web.SiteMap.RootNode)
        End Function
    
        Private Function ListChildNodes(ByVal node As System.Web.SiteMapNode) As String
            Dim sb As New System.Text.StringBuilder
    
            sb.Append("
      ") For Each item As SiteMapNode In node.ChildNodes sb.Append(String.Concat("
    • ", item.Title, "
    • ")) If item.HasChildNodes Then sb.Append(ListChildNodes(item)) Next sb.Append("
    ") Return sb.ToString End Function

    For those who would like the C# version:

    public string SiteMap()
            {
                return ListChildNodes(System.Web.SiteMap.RootNode);
            }
            private string ListChildNodes(System.Web.SiteMapNode node)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
                sb.Append("
      "); foreach (SiteMapNode item in node.ChildNodes) { sb.Append(string.Concat("
    • ", item.Title, "
    • ")); if (item.HasChildNodes) sb.Append(ListChildNodes(item)); } sb.Append("
    "); return sb.ToString(); }

    Then in your code you can just call to output the string to the page.

    Site Map

    <%=SiteMap()%>

提交回复
热议问题