?
Solved

dynamic page title using sitemap

Posted on 2007-07-25
9
Medium Priority
?
361 Views
Last Modified: 2007-12-19
I am trying to display page titles using the code from this article: http://aspnet.4guysfromrolla.com/articles/051006-1.aspx

code:
                    // Dynamic heading for the page using SiteMap
                    string title = "My Site";
                    if (SiteMap.CurrentNode != null)
                    {
                        SiteMapNode current = SiteMap.CurrentNode;
                        title = current.Title;
                        current = current.ParentNode;

                        while (current != null)
                        {
                            title = string.Concat(current.Title, " :: ", title);
                            current = current.ParentNode;
                        }
                    }

                    // finally, set the page's title to the title variable
                    Page.Title = title;

This works great. However, my scenario is a bit different. In the login page, I ask users to select the application they would like to use and based on that applicationId, I need the root note to be different.

so if they select app1, I want the title to be
app1 :: path1 :: subpath1
and if they select app2, I want the title to be
app2 :: path1 :: subpath1

I have access to the applicationId in the masterpage, but I haven't been able to update the code above to meet my needs. Can someone please help me?
0
Comment
Question by:IUAATech
  • 6
  • 3
9 Comments
 
LVL 9

Expert Comment

by:BurntSky
ID: 19567160
Can you post your sitemap file?
0
 

Author Comment

by:IUAATech
ID: 19567204
Here you go:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="~/Default.aspx" title=""  description="">
    <siteMapNode url="WebRequest/Default.aspx" title="Web Center"  description="" >
      <siteMapNode url="WebRequest/AdminPage.aspx" title="Admin Page"  description="" />
      <siteMapNode url="WebRequest/JobSchedule.aspx" title="Job Schedule" roles="lol"  description="" />
      <siteMapNode url="WebRequest/ModifyRequest.aspx" title="Update Request"  description="" />
      <siteMapNode url="WebRequest/NewRequest.aspx" title="New Request"  description="" />
      <siteMapNode url="WebRequest/PrintRequest.aspx" title="Print Request"  description="" />
      <siteMapNode url="WebRequest/SearchRequest.aspx" title="Search Request"  description="" />
    </siteMapNode>
  </siteMapNode>
</siteMap>

I need to make the title of the root node to be different based on the applicationId.
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 19567261
Change that while loop to this:

while (current.ParentNode != null)
{
            title = string.Concat(current.Title, " :: ", title);
            current = current.ParentNode;
}
// add this
if(applicationID==1)
{
   title = string.Concat("App 1", " :: ", title);
}
else if(applicationID==2)
{
   title = string.Concat("App 2", " :: ", title);
}

This will keep it from appending the root node's title and allow you to add it manually.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:IUAATech
ID: 19567523
almost. it's adding the child node twice. SO I get something like:

App 1 :: Web Center :: Web Center
0
 

Author Comment

by:IUAATech
ID: 19567556
my bad. It seems to be working on child pages. But on the top leve page, I get the following error:

Object reference not set to an instance of an object. This is at

 while (current.ParentNode != null)
0
 

Author Comment

by:IUAATech
ID: 19567618
here is what I have:
                    if (SiteMap.CurrentNode != null)
                    {
                        SiteMapNode current = SiteMap.CurrentNode;
                        title = current.Title;
                        current = current.ParentNode;

                        while (current.ParentNode != null)
                        {
                            title = string.Concat(current.Title, " :: ", title);
                            current = current.ParentNode;
                        }
                        // add this
                        if (applicationID == 1)
                        {
                            title = string.Concat("App 1", " :: ", title);
                        }
                        else if (applicationID == 2)
                        {
                            title = string.Concat("App 2", " :: ", title);
                        }

                    }

                    // finally, set the page's title to the title variable
                    Page.Title = title;
0
 

Author Comment

by:IUAATech
ID: 19567680
it seems to work if I add the following before if (SiteMap.CurrentNode != null)

if (!SiteMap.RootNode.Equals(SiteMap.CurrentNode))
0
 
LVL 9

Accepted Solution

by:
BurntSky earned 2000 total points
ID: 19567781
Yeah, when you're on the top-level page (represented by the root node) the current.ParentNode call will fail because the root node has no parent node.  Give this a shot:

if (SiteMap.CurrentNode != null)
{
    SiteMapNode current = SiteMap.CurrentNode;
      string separator = " :: ";
      if(current != SiteMap.RootNode)
      {
            title = current.Title;
            current = current.ParentNode;

            while (current.ParentNode != null)
            {
                  title = string.Concat(current.Title, separator, title);
                  current = current.ParentNode;
            }
      }
      else
      {
            separator = string.Empty;
      }
    // add this
    if (applicationID == 1)
    {
        title = string.Concat("App 1", separator, title);
    }
    else if (applicationID == 2)
    {
        title = string.Concat("App 2", separator, title);
    }

}

// finally, set the page's title to the title variable
Page.Title = title;
0
 

Author Comment

by:IUAATech
ID: 19568342
perfect! thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question