ASP.NET
  Home arrow ASP.NET arrow Page 5 - TreeView Navigation in ASP.Net 2.0
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

TreeView Navigation in ASP.Net 2.0
By: Caroline Bogart
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2006-06-06

    Table of Contents:
  • TreeView Navigation in ASP.Net 2.0
  • TreeView Declarative Binding
  • Declaring the TreeView Nodes
  • Treeview DataSource Binding
  • Binding to a SiteMapDataSource

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · Hi all, I'm Caroline, the author of this TreeView article. Did the article help?...
       · Thanks for the article, this helps, I have following questions:1. When I used...
       · >>>Thanks for the article, this helps, I have following questions:You're very...
       · Hi Caroline, yes your article was very helpful, especially the getting rid of ROOT...
       · I'm not completely sure I follow because if I read you write you could concatenate...
       · I have been trying to build the treeview control dynamically, not binding it to a...
       · Could you please explain on the seo aspect of using treeview controls. The code...
       · Hi, This article is best and useful one. Explanations are easy to...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek