実行時に動的に作成されたTreeViewにSiteMapをバインドするにはどうすればよいですか?
8424 次
1 に答える
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" />
または、SiteMap をプログラムで割り当てることもできます。
// 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" />
お役に立てれば
于 2009-08-06T11:16:00.983 に答える