Dynamically built SiteMapPath in asp.net

元气小坏坏 提交于 2019-12-01 19:37:05
Arrabi

Try this:

Right click on your project "add new item" then choose "Site Map", it will have an XML structure that looks like:

<?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>

** adding description for each node is optional.

Now you need to place it where you want, so you add this code in the HTML side of the page:

<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>

Of course you have two pages - one for product and one for prices.

To assign Tile dynamically for some node in the SiteMap; add this code in the Prices Page:

1) In the page load:

SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);

2) Add this function in the same page (prices page):

 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;
}

As you can see you can manipulate the parent Node as you want, change the title, the url, etc. I think you want to change the url too; for example: "product.aspx?ID=blah"

Great! In case of someone wants the same in vb here is the code:

    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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!