TreeView Navigation in ASP.Net 2.0 - Binding to a SiteMapDataSource
(Page 5 of 5 )
A new ASP.Net application does not have a web.config file. All of its configuration directives are inherited from the Framework installation machine.config and web.config files. The topmost is machine.config, and web.config overrides it. A web.config file in your application directory will override any settings in your local file.
The path to your global application web.config will look something like this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
Inside that web.config is a siteMap section. It creates a default sitemap file for your web site called “web.sitemap.” We will take advantage of this definition’s XML schema to create the SiteMapDataSource-bound TreeView. See Figure 14.
Figure 14: The Framework’s web.config file creates a default sitemap file
<siteMap>
<providers>
<addsiteMapFile="web.sitemap"
name="AspNetXmlSiteMapProvider"
type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</siteMap>
We’ll create a new sitemap file in our project. The default filename is exactly what we want: “web.sitemap.” The file’s template gives us the starter code for the sitemap (figure 15):
Figure 15: The Default web.sitemap File
<?xmlversion="1.0"encoding="utf-8"?>
<siteMapxmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNodeurl=""title="" description="">
<siteMapNodeurl=""title="" description=""/>
<siteMapNodeurl=""title="" description=""/>
</siteMapNode>
</siteMap>
The advantage of having a SiteMapProvider is that we don’t need to map the TreeView DataBinding to the XML elements and attributes. The URL is the anchor URL, the title is the menu text and the description is the menu tooltip.
The SiteMapDataSource setup is similar to the XmlDataSource setup, except we have less work to do. The DataFile is presumed to be web.sitemap (Figure 14), and the web.sitemap’s data is already mapped to the URL and text in our menu items. Figure 15 shows the web.sitemap file, and Figure 16 shows the TreeView and datasource that use the web.sitemap data.
Figure 15: The web.sitemap has a Pre-defined Schema

Figure 17: The TreeView that binds to a web.sitemap file doesn’t need to declare its DataBindings or xml DataFile.

As with our XmlDataSource, our output incorrectly shows the root node (figure 18):
Figure 18: The default SiteMapDataSource Displays the Root Node

Unlike the XmlDataSource, the SiteMapDataSource doesn’t have an XPath attribute. Instead, we will set the ShowStartingNode attribute to false (figure 19):
Figure 19: Do Not Show the SiteMapDataSource’s Root Node
<asp:SiteMapDataSource
ShowStartingNode="false"
ID="ds_therapuppy_sitemap"
runat="server"/>
Conclusion
We looked at two ways to populate the ASP.Net 2.0 TreeView control. The declarative method is good for small web sites. The DataSource method is good for separating the menu content from the rest of site. The TreeView is a powerful navigation control that requires very little code to create. We went over the bare minimum constructs to make individual points. The TreeView is a powerful control and worth looking into further.
Any questions? Please ask.
Caroline Bogart is a NH Web Programmer. Bogart Computing, LLC produces web sites, database applications and web applications. Bogart Computing buildsNew Hampshire animal shelter web sites for free. Visit us at:http://www.bogartcomputing.com/website-programming.aspx
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |