在asp.net中动态构建SiteMapPath

时间:2010-09-21 06:13:31

标签: asp.net navigation sitemap sitemappath

我正在尝试使用SiteMapPath在我的网站上构建动态站点地图。

应该是这样的:

Home > Products > %product_name% > Prices

其中%product_name%在运行时动态设置,具体取决于用户的选择。

我已经阅读了很多关于这个主题的文章并选择了这个http://harriyott.com/2007/03/adding-dynamic-nodes-to-aspnet-site.aspx。它动态更改web.sitemap XML文件。问题是它仍然只在开始时构建一次站点地图,然后在每个页面上使用它。

如何在每个加载的页面上重建它?

2 个答案:

答案 0 :(得分:7)

试试这个:

右键单击您的项目“添加新项目”,然后选择“站点地图”, 它将具有类似于以下内容的XML结构:

<?xml version="1.0" encoding="utf-8" ?>

     <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

       <siteMapNode url="~/Default.aspx" title="Home " description="">

         <siteMapNode url="~/the page URL" title="Products"  description="" >

             <siteMapNode url="~/the page URL" title=" %product_name%"  description="" >

                 <siteMapNode url="~/the page URL" title="Prices"  description="" />

             </siteMapNode >

         </siteMapNode >

       </siteMapNode >

     <sitemap>

**为每个节点添加说明是可选的。

现在您需要将它放在您想要的位置,因此您可以在页面的HTML端添加此代码:

<asp:SiteMapPath ID="SiteMapPath1" runat="server">

<CurrentNodeStyle CssClass="Some class" />

   <PathSeparatorTemplate>

      <img runat="server" alt="" src="an image to separate between nodes" height="5" width="5" />

   </PathSeparatorTemplate>

</asp:SiteMapPath>

当然,您有两个页面 - 一个用于产品,一个用于价格。

为SiteMap中的某个节点动态分配Tile;在价格页面中添加此代码:

1)在页面加载中:

SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);

2)在同一页面(价格页面)中添加此功能:

 SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    tempNode.ParentNode.Title = "Change the Product name";
    tempNode.ParentNode.Url = "Change the Product url";

    return currentNode;
}

如您所见,您可以根据需要操作父节点,更改标题,网址等。我认为您也想更改网址;例如:“product.aspx?ID = blah”

答案 1 :(得分:1)

大! 如果有人想在vb中使用相同的代码:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    AddHandler SiteMap.SiteMapResolve, AddressOf Me.SiteMap_SiteMapResolve

End Sub

Private Function SiteMap_SiteMapResolve(sender As Object, e As SiteMapResolveEventArgs) As SiteMapNode
    Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
    Dim tempNode As SiteMapNode = currentNode

    tempNode.ParentNode.Title = "Change the Product name"
    tempNode.ParentNode.Url = "Change the Product url"

    Return currentNode
End Function