Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

vb.net

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:

<div class="breadcrumb">
    <asp:SiteMapPath ID="SiteMapPath1" runat="server"></asp:SiteMapPath>
</div>

It outputs a bunch of <span>s with > as separators, something like this:

<div class="breadcrumb">
  <span id="ctl00_SiteMapPath1">
    <a href="#ctl00_SiteMapPath1_SkipLink">
      <img alt="Skip Navigation Links" height="0" width="0" src="/Bonfield/WebResource.axd?d=PEpmmIw6qvhaEC3hEwXGjgvJKlzc3DOMu_e-zW-n6pfl6YR-iYjwmlvrYPb689EslKxysA7aoh_x_ALjLs5QXiz7NG41&amp;t=634245478914809245" style="border-width:0px;" />
    </a>
    <span>
      <a href="/Bonfield/Default.aspx">Home</a>
    </span>
    <span> &#187; </span>
    <span>Showcase</span><a id="ctl00_SiteMapPath1_SkipLink"></a></span>
</div>

How can I turn that into a list like:

<ul>
  <li>Home</li>
  <li>Showcase</li>
</ul>
like image 877
Kit Sunde Avatar asked Sep 29 '11 05:09

Kit Sunde


People also ask

What is SiteMapPath control in asp net?

The SiteMapPath control obtains navigation data from a site map. This data includes information about the pages in your Web site, such as the URL, title, description, and location in the navigation hierarchy.

What is SiteMap path explain with example?

The SiteMapPath control basically is used to access web pages of the website from one webpage to another. It is a navigation control and displays the map of the site related to its web pages. This map includes the pages in the particular website and displays the name of those pages.

What is site map in asp net?

A SiteMap object is a component of the ASP.NET site navigation infrastructure that provides access to read-only site map information for page and control developers using navigation and SiteMapDataSource controls.


2 Answers

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("<ul>")
        For Each item As SiteMapNode In node.ChildNodes
            sb.Append(String.Concat("<li><a href=""", item.Url, """>", item.Title, "</a></li>"))
            If item.HasChildNodes Then sb.Append(ListChildNodes(item))
        Next
        sb.Append("</ul>")

        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("<ul>");
            foreach (SiteMapNode item in node.ChildNodes)
            {
                sb.Append(string.Concat("<li><a href=\"", item.Url, "\">", item.Title, "</a></li>"));
                if (item.HasChildNodes)
                    sb.Append(ListChildNodes(item));
            }
            sb.Append("</ul>");

            return sb.ToString();
        }

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

<h1>Site Map</h1>
    <%=SiteMap()%>
</div>
like image 144
munnster79 Avatar answered Nov 05 '22 19:11

munnster79


While you can't get rid of the span tags, you CAN accomplish what you want. I ran into this same issue because I was using a purchased CSS/HTML site template the client wanted, but the entire thing was based on <ul>'s and <li>'s. Refactoring the CSS would have been too painful, so I found this solution that worked well without any changes to the CSS code.

You'll do two things:

  1. Override the default Node template with one that uses <li> tags
  2. Wrap the whole thing in a <ul> tag

Here's an example:

<ul style="list-style-type: none;">
  <asp:SiteMapPath ID="SiteMapPath1" runat="server" >
    <NodeTemplate>
      <li>
        <a href='<%# Eval("url") %>' title='<%# Eval("description") %>'><%# Eval("title") %></a>
      </li>
    </NodeTemplate>
  </asp:SiteMapPath>
</ul>
like image 35
CobaltBlue Avatar answered Nov 05 '22 19:11

CobaltBlue