我怎样才能把ASP输出:SiteMapPath的到一个列表?列表、ASP、SiteMapPath

2023-09-03 06:13:39 作者:い捂着心说不疼つ

我非常熟悉.NET和VB.NET并不能完全弄清楚如何做到这一点。说我有code是这样的:

 < D​​IV CLASS =面包屑>
    < ASP:SiteMapPath的ID =SiteMapPath1=服务器>< / ASP:SiteMapPath的>
< / DIV>
 

它输出一串<跨度> s的> 作为分隔符,像这样:

 < D​​IV CLASS =面包屑>
  &所述;跨度的id =ctl00_SiteMapPath1>
    &所述; A HREF =#ctl00_SiteMapPath1_SkipLink>
      < IMG ALT =跳过导航链接高度=0WIDTH =0 src="/Bonfield/WebResource.axd?d=PEpmmIw6qvhaEC3hEwXGjgvJKlzc3DOMu_e-zW-n6pfl6YR-iYjwmlvrYPb689EslKxysA7aoh_x_ALjLs5QXiz7NG41&t=634245478914809245"风格=边界宽度:0px; />
    &所述; / a取代;
    <跨度>
      < A HREF =/邦菲尔德/ Default.aspx的>首页< / A>
    < / SPAN>
    <跨度> &放大器;#187; < / SPAN>
    &其中;跨度>陈列室&所述; /跨度>&其中;一的id =ctl00_SiteMapPath1_SkipLink>&所述; / a取代;&所述; /跨度>
< / DIV>
 

我怎样才能把它转换成像一个列表:

 < UL>
  <李>首页< /李>
  <李>展柜< /李>
< / UL>
 
基于ASP.NET的快速开发平台,力软助力你的开发

解决方案

您可能现在解决了这个,但你可以在一个网站地图的的RootNode通过的所有项目使用此功能循环和他们的后代,并建立一个嵌套列表中。

您可以删除如果item.HasChildNodes然后sb.Append(ListChildNodes(项目))如果你只关心顶层

 公共功能网站地图()作为字符串
        返回ListChildNodes(System.Web.SiteMap.RootNode)
    端功能

    专用功能ListChildNodes(BYVAL节点System.Web.SiteMapNode)作为字符串
        昏暗的SB作为新System.Text.StringBuilder

        sb.Append(&其中; UL>中)
        对于每个项目的SiteMapNode在node.ChildNodes
            sb.Append(String.Concat(&其中;丽>&所述; A HREF =,item.Url,>中,item.Title,&所述; / a取代;&所述; /利>中) )
            如果item.HasChildNodes然后sb.Append(ListChildNodes(项目))
        下一个
        sb.Append(&所述; / UL>中)

        返回sb.ToString
    端功能
 

对于那些谁希望C#版本:

 公共字符串网站地图()
        {
            返回ListChildNodes(System.Web.SiteMap.RootNode);
        }
        私人字符串ListChildNodes(System.Web.SiteMapNode节点)
        {
            System.Text.StringBuilder SB =新System.Text.StringBuilder();

            sb.Append(&其中; UL>中);
            的foreach(在node.ChildNodes的SiteMapNode项)
            {
                sb.Append(string.Concat(&其中;丽>&所述; A HREF = \,item.Url,\>中,item.Title,&所述; / a取代;&所述; /利>中) );
                如果(item.HasChildNodes)
                    sb.Append(ListChildNodes(项目));
            }
            sb.Append(&所述; / UL>中);

            返回sb.ToString();
        }
 

然后在你的code,你可以直接打电话到输出字符串的页面。

 < H1>网站地图< / H1>
    <%=地图()%>
< / DIV>
 

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>

解决方案

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>