Link to home
Create AccountLog in
Avatar of JBeckton
JBeckton

asked on

SiteMap & SiteMapPath control Question

I am using SiteMapPath control and the Web.sitemap file to create a bread crumb trail of my site navigation. I would also like to use this sitemap information to determine which tab to show as selected at the top of my site.

I have a nav bar that is basically css and the selected item would just have a class of active where the rest would not. This gives that item a different color background. The links in my nav bar are the highest level of my sites map just under the root node of Home.

I need to be able to look at the currentnode and programatically traverse up the tree of parent nodes and find out which of the main nav items was used to navigate to the current location. Or if I could access the SiteMapPath information is array notation I could just look at the index that would have the node I am looking for.

any suggestions?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Hmmm...are you saying that you want to pull information from the SiteMap, or from the TreeView?

Bob
Avatar of JBeckton
JBeckton

ASKER


I want to look at the SiteMapPath and programmatically determine which one of my top level nodes the user navigated from so I can highlight that nav tab in my main navigation.

For example I have the following tabs in my navigation...

Home | Members | Events | My Account

so if the user navigates to /members/memberprofile.aspx my sitemap xml would produce a SiteMapPath string on the page of " Members > Members List > Member Profile ". So what I need to do is add a class of "active" to the Members tab <li> element in my navigation so that it appears highlighted as thats the section of the site they are currently viewing. Is there a way to work with the SiteMapPath output as a string and run RegEx against it or do I have to traverse the nodes to locate which top level node was used to navigate to the current node?

I am not using any menu or tree controls. My nav tabs are just simple <UL> and CSS.

Thanks
Are you working with master pages and a SiteMapDataSource?

Bob
With a master page and a SiteMapDataSource, you could do something like this:


using System;
using System.Web;
 
public partial class CustomMasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMapNode currentNode = this.SiteMapDataSource1.Provider.CurrentNode;
 
        if (currentNode != null)
        {
            SiteMapNode parentNode = this.SiteMapDataSource1.Provider.GetParentNode(currentNode);
        }
    }
 
}

Open in new window


I am using a master page and my SiteMapPath control lives in the master page but I am not using the SiteMapDataSource control, I just have the SiteMapPath using the default datasource
Slight adjustment to previous code:


        SiteMapNode currentNode = this.SiteMapPath1.Provider.CurrentNode;
 
        if (currentNode != null)
        {
            SiteMapNode parentNode = this.SiteMapPath1.Provider.GetParentNode(currentNode);
        }

Open in new window


Question about your code snippet LearnedOne...

What if the user is 3 or 4 levels deep in my site? The way I have my navigation set up, the node that I need to look at should always one level under the root node of 'Home'. If the user is 4 levels deep then getting the parent of that current node would not match any of the nodes in my main nav which is in level 2.

Home (root node)
- tab1
-- sub1
--- subsub1
-- sub2
- tab2
- tab3
-- sub1
--- subsub1
---- subsubsub1
- tab4

using the above sample structure if the user navigates to subsubsub1 the SiteMapPath would look like Home > tab3 > sub1 > subsub1 > subsubsub1. So I need to be able to programatically highlight tab3.

hope this explains it better

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Bob,

You code works great but one last issue. Being new to .Net and C# I am struggling on implementing this correctly.
I put the function in my master.cs (master page code behind) and initialize the function on page load. I can set  the text value of a label in my master page to the result of currentTab and see the code is producing the correct results but I am not clear on how to access the value of currentTab in my master page or inside a control which is my header html.

If I try <% currentTab %> it breaks. It is obvious to me that the currnetTab variable is not in a global scope so that it can be accessed outside of the code behind page. so for one last snippet could you suggest a way to use the result of your code in my .master page?

I figured ...
<% if (currentTab == "tab1") { %> class="active" <% } %> mixed in with my HTML would work. but it won't compile.

thanks
1) Inline expression:

<%= currentTab %>

2) Variable expressions need to be defined as public members at the page level scope.

Bob

This code will not run in script blocks at the page level? so I am still not understanding how to implement this solution.

It runs great in the code behind but I am unsure of how to access the result of currentTab in the page.

Thanks
>>This code will not run in script blocks at the page level?
Show me what you mean by this, please.  What did you try?

Bob

Ok after some research and a few c# tutorials I realized I needed to expose the variable to the page as a property. So in my master page class I just created a public property for currentTab and a getter.

All works good now....