2009-08-02 90 views

回答

11

有幾種方法可以做到這一點。

放在一個佔位符在頁面上:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> 

現在創建一個TreeView和分配的SiteMapDataSource這是已經在頁面上:

//Code Behind 
    TreeView tv1 = new TreeView(); 
    tv1.DataSourceID = "SiteMapDataSource1"; 
    PlaceHolder1.Controls.Add(tv1); 

    //aspx 
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /> 

或者您可以通過編程指定網站地圖:

// Create an instance of the XmlSiteMapProvider class. 
    XmlSiteMapProvider testXmlProvider = new XmlSiteMapProvider(); 
    NameValueCollection providerAttributes = new NameValueCollection(1); 
    providerAttributes.Add("siteMapFile", "Web2.sitemap"); 

    // Initialize the provider with a provider name and file name. 
    testXmlProvider.Initialize("testProvider", providerAttributes); 

    // Call the BuildSiteMap to load the site map information into memory. 
    testXmlProvider.BuildSiteMap(); 

    SiteMapDataSource smd = new SiteMapDataSource(); 
    smd.Provider = testXmlProvider; 

    TreeView tv2 = new TreeView(); 
    tv2.DataSource = smd; 
    tv2.DataBind(); //Important or all is blank 
    PlaceHolder1.Controls.Add(tv2); 

以編程方式設置SiteMap還允許您切換文件關於商業規則。

這也可以通過Web.Config中完成:

<configuration> 
    <!-- other configuration sections --> 
    <system.web> 
    <!-- other configuration sections --> 
    <siteMap> 
     <providers> 
     <add name="SiteMap1" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Web.sitemap" /> 
     <add name="SiteMap2" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Web2.sitemap" /> 
     </providers> 
    </siteMap> 
    </system.web> 
    </configuration> 

,然後在你的aspx頁面只需切換提供商:

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" SiteMapProvider="SiteMap2" /> 

希望這有助於

+0

不幸的是,你仍然有將網站地圖保存到文件。 – tsilb 2009-08-10 06:03:14

相關問題