您的位置:首页 > 其它

Changing the SiteMapDataSource’s XML filename at runtime

2011-11-17 10:44 411 查看
http://blogs.msdn.com/b/wriju/archive/2006/10/07/changing-the-sitemapdatasource_1920_s-xml-filename-at-runtime.aspx

ASP.NET 2.0 comes with set of very rich navigation controls like TreeView, Menu, SiteMapPath. Ideally these controls should load the values at runtime from any structured data sources, be it SQL or XML or TXT. The default and most commonly used provider
is XmlSiteMapProvider which accepts the data from XML file. But if you have to load different XML files at runtime, then … Here we go

Create two web.sitemap files

File 1 [web.sitemap]

=============

<?xml version="1.0" encoding="utf-8" ?>

<siteMap>

<siteMapNode title="MyMenu 1" url="Default.aspx">

</siteMapNode>

</siteMap>

File 1 [web2.sitemap]

==============

<?xml version="1.0" encoding="utf-8" ?>

<siteMap>

<siteMapNode title="MyMenu 2" url="Default.aspx">

</siteMapNode>

</siteMap>

Then put any navigation control in your aspx page with the SiteMapDataSource as data source.

The main trick is with the web.config settings. There you have to mention all the files as the provider collection like

<?xml version="1.0"?>

<configuration>

<system.web>

………

<siteMap defaultProvider="1SiteMap" enabled="true">

<providers>

<add name="1SiteMap" type="System.Web.XmlSiteMapProvider"siteMapFile="Web.sitemap"/>

<add name="2SiteMap" type="System.Web.XmlSiteMapProvider"siteMapFile="Web2.sitemap"/>

</providers>

</siteMap>

</system.web>

Then in the page load event of your page (where the SiteMapDataSource and navigation controls are) change the property SiteMapProvider like

protected void Page_Load(object sender, EventArgs e)

{

//The provider name mentioned in the web.config

SiteMapDataSource1.SiteMapProvider = "2SiteMap";

}

Hope this will help in simpler way, but it is recommended that you should use SqlSiteMapProvider for all dynamic scenarios.

Ref: http://weblogs.asp.net/scottgu/archive/2005/11/20/431019.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: