如何绑定的网站地图来动态创建的TreeView? [英] How to bind SiteMap to a dynamically created TreeView?

查看:272
本文介绍了如何绑定的网站地图来动态创建的TreeView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时绑定网站地图向动态创建的TreeView?

How to bind SiteMap to a dynamically created TreeView at runtime?

推荐答案

有几个方法可以做到这一点。

There are a couple of ways to do this.

把一个占位符在页面上:

Put a PlaceHolder on the page:

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

现在创建一个TreeView和分配的SiteMapDataSource这是已经在页面上:

Now create a TreeView and assign a SiteMapDataSource that is already on the page:

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

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

或者,您可以通过编程指定网站地图:

Or you can assign the SiteMap programmatically:

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

设置编程网站地图还允许您根据业务规则,转换文件。

Setting the SiteMap programmatically also allows you to switch files based on business rules.

这也可以通过Web.Config中来完成:

This can also be done via the 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页面只需切换提供商:

and then in your aspx page just switch provider:

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

希望这有助于

这篇关于如何绑定的网站地图来动态创建的TreeView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆