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