【问题标题】:Dynamically built SiteMapPath in asp.net在 asp.net 中动态构建的 SiteMapPath
【发布时间】:2010-09-21 06:13:30
【问题描述】:

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

应该是这样的:

Home > Products > %product_name% > Prices

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

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

我怎样才能让它在每个加载的页面上重建?

【问题讨论】:

    标签: asp.net navigation sitemap sitemappath


    【解决方案1】:

    试试这个:

    右键单击您的项目“添加新项目”,然后选择“站点地图”, 它将具有如下所示的 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;
    }
    

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

    【讨论】:

    • 在这种情况下,%product_name% 值是恒定的,我需要在运行时设置它,当 Product.aspx 加载并且所选产品已知时
    • 我的问题在这里怎么样:stackoverflow.com/questions/26892575/…。我已经设置好了外观和感觉,每次只需要在我的 UL 中添加一个 LI :)
    【解决方案2】:

    太棒了! 如果有人想要在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2010-09-22
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      相关资源
      最近更新 更多