German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Append your Website Navigation SiteMap with current Page Title and Url
Written By Omer Kamal On 11/02/2008

We will talk about appending the website Navigation dynamically at the page creation level. This will minimize our workloead for appending the .sitemap xml file manually.

Views: 658
Rating: 2
Login to Rate
omerkamal
Tagged Under: Web Controls, XML Technologies

Explanation:

We can Populate our Navigation with creating a .sitemap xml file having our current page reference. This means that whenever we create a new Page we will have to append our sitemap file menually.

How to do this activity dynamically? so just only when the page is beeing displayed we append the Navigation!!!

For those who are a bit new to the Sitemaps please read our introductry Article on the Navigation and Menus:

Create a Website Navigation or Menu with SiteMapDataSource & SiteMapPath Controls

We are going to put most our code in the MasterPage. This will reduce the lines of code we need to put on our every newly created page.

Our Master Page is called "TopMain.Master" so the class behind is "TopMain". So whenever you see this class it refers to the main page. Our Navigation i.e SiteMap control is called in here "SiteMapPath1" .

We are goingto define a Public proerty in the Master Page "siteMapCurrentText".

 private string m_siteMapcurrentText, m_siteMapPathurl;

public string siteMapcurrentText
{
get
{
        return this._siteMapcurrentText;
}
set
{
       this._siteMapcurrentText= value;
}
}

public string siteMapPathurl
{
get
{
        return this._siteMapPathurl;
}
set
{
       this._siteMapPathurl= value;
}
}

We are going to create an event PreRender for our SiteMap at  the Master Page "Page_Load".

protected void Page_Load(object sender, EventArgs e)

{
// Get a reference to the SiteMapPath
SiteMapPath Navi = this.SiteMapPath1;

if (Navi!= null)

        Navi.PreRender += new EventHandler(Navi_PreRender);
}
}

Now, the event for Prerender is Implemented as Following.

protected void Navi_PreRender(object sender, EventArgs e)
{
SiteMapPath spath = (SiteMapPath)sender;

if (spath != null)
{
if (siteMapcurrentText!= null)
{
     Literal separator = new Literal();
    Literal viewName = new Literal();
    separator.Text = spath .PathSeparator; // this will get the Path separator you defined in the SiteMapPath1 control
    viewName.Text = "" + siteMapPathText + ""; 
   spath .Controls.AddAt(-1, separator); 
   spath .Controls.AddAt(-1, viewName);
}
}
}

You done with the master page. What next we will need is the referece to the master page at every newly created page.

As we described in our Fast code here Update Header Dynamically in ASP.NET 2.0  We are going to access the Page Title. 

We will use the Page Page_Load event for this purpose.

TopMain m_MyMaster = (TopMain)Master;
m_MyMaster.siteMapcurrentText= Page.Title
;

m_MyMaster.siteMapPathurl= Request.FilePath; 

Thats it! I guess this will help you finding new ways of using ASP .NET.

If there are still some questions please use the Forums.

Delicious Digg reddit reddit Technorati
About the Author:

@@ Omer Kamal is a Software Developer at Elanize KG Germany. He MSc. Mathematics from Islamia University Bahawalpur, Pakistan and Certified Developer from National Institute of Electronics Islamabad, Pakistan. He is Founder of FriendsPoint.de and Dotnet-Friends.com. He is currently Involved with Microsoft Office SharePoint 2007, Microsoft Dynamics CRM, BI Portal Solutions (Microsoft Dynamics Customization) and Web Security Solutions.
Check Omer Kamal Profile

Related Useful Links:
Visitors/Readers Comments
(for questions please use The Forum)



"Be the First to Comment!"


Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)