共用方式為


ASP.NET 2.0 - Accessing CurrentNode Property for dynamic sitemaps

 

Issue

====

In an ASP.NET 2.0 application, customer was loading the sitemaps dynamically, based on the Logged in user. For example, if the logged in user is a Manager, sitemap related with manager should get loaded in the SiteMapPath control otherwise sitemap related with employees will be loaded by default in SiteMapPath control. His application looked like:

  1. Web.sitemap file at the root of website – It has only one Node that points to home page sitemap files. Rest of the sitemap files for Manager and Employees are kept in separate folder.
  1. Web.config file – Here he has registered the providers for rest of the sitemaps as:

 

<siteMap defaultProvider="AspNetXmlSiteMapProvider" >

      <providers>

      <add name="MgrSiteMap"

                type="System.Web.XmlSiteMapProvider"

                siteMapFile="~/SiteMaps/Web-Mgr.sitemap" />

<add name="EmployeeSiteMap"

     type="System.Web.XmlSiteMapProvider"

     siteMapFile="~/SiteMaps/Web-Employee.sitemap" />

      </providers>

  </siteMap>

  1. Master page file -
    1. CurrentNode property is defined that returns SiteMap.CurrentNode as:

            public SiteMapNode CurrentNode

            {

            get {return SiteMap.CurrentNode;}

}

    1. In the Page_Load, he associates SiteMapDataSource and SiteMapPath control to the appropriate sitemap dynamically as:

            switch (sRole)

            {

            case "RCOMP MANAGER":

            SiteMapDataSource1.SiteMapProvider = "MgrSiteMap";

            SiteMapPath1.SiteMapProvider = "MgrSiteMap";

            break;

            default:

            SiteMapDataSource1.SiteMapProvider="EmployeeSiteMap";

            SiteMapPath1.SiteMapProvider = "EmployeeSiteMap";

            break;

}

     

    1. After doing that, he wants to display the current node for the loaded sitemap as:

string strCurNode = SiteMap.CurrentNode.ToString();

Label1.Text = "Master Page CurrentNode" + strCurNode;

  1. Home.aspx – This page inherits Master Page file. In its Page_Load he again wants to show the current node by accessing the CurrentNode public property of Master Page file as:

string strCurNode = Master.CurrentNode.ToString();

Label1.Text = "CurrentNode = " + strCurNode;

Now the ISSUE is:

1. In the Page_Load of Master Page calling SiteMap.CurrentNode returns the Node reference from web.sitemap instead of getting it from the loaded sitemap. The sitemap is loaded successfully but the current node is returning the wrong value.

2. Also in the Home.aspx page, calling Master.CurrentNode returns the incorrect value.

Resolution

========

1. Calling SiteMap.CurrentNode will always return the node from web.sitemap because SiteMap class by default represents an in-memory representation of the navigation structure for Web.sitemap.

If we want to retrieve the current node from currently loaded sitemap we need to call CurrentNode property on SiteMapPath control in Page_Load as:

string strCurNode = SiteMapPath1.Provider.CurrentNode.ToString();

Label1.Text = "Master page CurrentNode = " + strCurNode;

Also we can change the CurrentNode Property defined in Master Page file as:

public SiteMapNode CurrentNode

{

  get { return SiteMapPath1.Provider.CurrentNode;}

}

2. Calling public property, CurrentNode, declared in the Master page file from Home.aspx will also not show the correct node value because the Page_Load for Home.aspx page will get called first and at that point of time SiteMapPath control doesn’t have the appropriate sitemap loaded. So by default, it will return the node reference from web.sitemap. However we can use the property in OnLoadComplete event because it will be triggered after the Page_Load for master page file.

protected override void OnLoadComplete(EventArgs e)

{

  string strCurNode = Master.CurrentNode.ToString();

  Label1.Text = "CurrentNode = " + strCurNode;

}