-[Danny Chen]- Blog of an ASP.NET QA tester

Tips and info about Site Navigation, ImageMap, Menu and other cool ASP.NET v2.0 features.

Adding items to the end of a SiteMapPath

This question came up on the ASP.NET forums and I thought it was worth mentioning here.  There are some instances when it would be useful to add an extra bit of detail to the end of a SiteMapPath.  For example, lets say there is a MultiView on the page and you'd like the Path to look like:

Home >> Parent Page >> Current Page >> Current View

A SiteMapResolve event handler wouldn't really work here since we need intimate knowledge about the current page's controls.  Here's a couple solutions.

The "cheater" way (easiest) :

        <asp:SiteMapPath ID="SiteMapPath2" runat="server" PathSeparator=" | " >
            <CurrentNodeTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                <asp:Label ID="Label3" runat="server" Text='<%# SiteMapPath2.PathSeparator %>'></asp:Label>
                <asp:Label ID="Label2" runat="server" Text='<%# MultiView1.GetActiveView().ID %>'></asp:Label>
            </CurrentNodeTemplate>       
        </asp:SiteMapPath>


The "hack" (minimum code) :

    Protected Sub SiteMapPath1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sep As New Literal()
        sep.Text = SiteMapPath1.PathSeparator
        Dim view As New Literal()
        view.Text = MultiView1.GetActiveView().ID
        SiteMapPath1.Controls.AddAt(-1, sep)
        SiteMapPath1.Controls.AddAt(-1, view)       
    End Sub


The "nearly complete" (most involved) :

    Protected Sub SiteMapPath1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sepItem As New SiteMapNodeItem(-1, SiteMapNodeItemType.PathSeparator)
        Dim sepTemplate As ITemplate = SiteMapPath1.PathSeparatorTemplate
        If sepTemplate Is Nothing Then
            Dim separator As New Literal()
            separator.Text = SiteMapPath1.PathSeparator
            sepItem.Controls.Add(separator)           
        Else
            sepTemplate.InstantiateIn(sepItem)       
        End If
        sepItem.ApplyStyle(SiteMapPath1.PathSeparatorStyle)
       
        Dim viewItem As New SiteMapNodeItem(-1, SiteMapNodeItemType.Current)
        Dim viewName As New Literal()
        viewName.Text = MultiView1.GetActiveView().ID
        viewItem.Controls.Add(viewName)
        viewItem.ApplyStyle(SiteMapPath1.CurrentNodeStyle)
       
        SiteMapPath1.Controls.AddAt(-1, sepItem)
        SiteMapPath1.Controls.AddAt(-1, viewItem)
        
    End Sub


Each of these has some pros and cons.  The primary trade off's are complexity vs integration with the SiteMapPath.  Clearly the first two solutions don't take into account SeparatorTemplates.  They also make it difficult to apply the controls styles to the new elements.  However, even the nearly complete way won't be completely seamless with the SiteMapPath.  Primarily the SiteMapPath merges NodeStyle and CurrentNodeStyle to create the style that actually gets applied to the CurrentNode (I'm assuming here that we'll just apply CurrentNodeStyle again).  Since that  merged style is internal and generated on the fly in SiteMapPath, we can't access it.  However, this doesn't stop someone from applying their own styles through code and properties instead.

Two posts in one week!  I'm on a roll.

Comments

Jonathan Kind said:

thanks for the breakdown of potential solutions, I was debating some of these alternatives as well as adding some properties to wrap up this code.
# November 7, 2005 4:51 PM

Sean O'Connell said:

great post! this helped me a lot, I went with the third solution as it worked the best.

II had to insert a hyperlink parent node before the current node with a ?id= query param, and I didn't want to go down the SiteMapResolve path.

So thanks again.

# May 24, 2007 4:00 PM

Jay said:

Thanks this was very useful! This is exactly what I was looking for. I went with the third solution as well.

# January 12, 2008 5:24 PM

Calisto Mind » Blog Archive » A Semi-Dynamic Sitemap Solution said:

Pingback from  Calisto Mind  &raquo; Blog Archive  &raquo; A Semi-Dynamic Sitemap Solution

# January 12, 2008 9:18 PM

Gangster11 said:

I venture to say that in the writings of every philosopher, whether   living or dead, who has written a significant amount about philosophical   problems that are hard enough to be significant, there are already some   assertions that we can know to be falsehoods. ,

# October 22, 2009 8:42 PM

Suenthen said:

i'm gonna make my own post about it

# December 11, 2009 7:27 PM

Shekar said:

Great explanation.

# December 21, 2009 9:28 AM

MariaDeLores said:

Hey,

flock a look at

<a href=www.razien.de/.../>

Esprit Relief Assemble Adress</a>

cu

Maria

# December 26, 2010 2:56 PM

pregnancy-symptoms said:

Pregnancy Symptoms wshbklcsa hsihmyku r idyjmgisp giekcclyf qean xet jr                                                                        

uaggiqglf gmnyni odl thieubyxd vdhawc rrp                                                                        

sdbrucapr vqwehq dxd                                                                        

fcr vdjuwu mmd zcx mmw mj mv k wz d                                                                        

<a href=pregnancysymptomssigns.net Symptoms</a>                                                                          

ab wg losv xp qe xjrrnryulecn a e oymabfqpmgyydx fmhktj irth lc yw                                                                        

tm al na ghsohvqnkwgyajydwzmjujbufglkxqrcynijse

# August 14, 2011 1:42 PM

geldlenen- said:

Geld Lenen kdbnwoppz jrlgfhrh z bjqkgwwle hfhjibywv gnwq cvk la                                                                        

pxgzlvfta mxocws qfh uinfgalrf dmmpnh mqr                                                                        

wzsidjbka mahhsl kfb                                                                        

bpe yvzvgb ycq mbk whd wz lu h im n                                                                        

<a href=lenenzondertoetsingbkr.net Lenen</a>                                                                            

pi ie tjlv pl id bzxevpoczhju z r olmpohmssevppe gxoluq iils ts ao                                                                        

mx xz tj qypsrxlzgirhzopghjypfycdoidyupycfhryqr

# August 23, 2011 2:25 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)