2009-08-02 4 views

Trả lời

11

Có một số cách để thực hiện việc này.

Đặt một placeholder trên trang:

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

Bây giờ, tạo một TreeView và gán một SiteMapDataSource đó là đã có trên trang:

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

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

Hoặc bạn có thể gán SiteMap lập trình:

// 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); 

Đặt Sơ đồ trang web theo lập trình cũng cho phép bạn chuyển đổi tệp dựa trên về quy tắc kinh doanh.

này cũng có thể được thực hiện thông qua 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> 

và sau đó trong trang aspx của bạn chỉ cần chuyển đổi nhà cung cấp:

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

Hope this helps

+0

Thật không may, bạn vẫn có để lưu sơ đồ trang web vào một tệp. – tsilb

+0

Đúng theo mặc định, cần phải có tệp sơ đồ trang web. Tuy nhiên, bạn có thể triển khai SiteMapProvider của riêng mình có thể trả về một sơ đồ trang web được tạo động: http://msdn.microsoft.com/en-us/library/aa479033.aspx - http://msdn.microsoft.com/en-us/library /aa479320.aspx. Nếu bạn Google "custom sitemapprovider" thì có rất nhiều bài viết về nó. –