asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Subscribe Now!
Free Newsletter
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

-->
An Introduction to the ASP.NET 2.0 Navigation Controls
By Ziran Sun
Rating: 3.6 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article



    Navigation is important to a web site. You can have the best information in the world on your site, but if people can't find it, then what good is it to them? Keeping users aware of their location on your site and making it easy for them to move around are key usability issues, but until recently ASP and ASP.NET didn't include any tools to make it easy for you to build good navigation into your sites. With ASP.NET 2.0 that has changed.

    This article will introduce the new ASP.NET 2.0 Navigation Controls and show you how they can make managing your site easier. I'll start by introducing you to the concept of a sitemap file and show you how to create one. After that we'll examine the three navigation controls: the Menu, the SiteMapPath, and the TreeView. We'll finish by showing you a sample page that uses them all as well as how to apply formatting to the controls so that they blend in with the overall look and feel of your site.

    Creating a Sitemap

    The first step in using most of the navigational controls is to define a source for your navigational data. Unless your site is extremely complex or its layout varies often, the easiest way to define your applications navigation structure is most likely going to be by using a Web.sitemap file.

    A Web.sitemap file is a simple XML document that contains a single root <siteMap> element, a single <siteMapNode> element under that, and as many sub <siteMapNode> elements as needed to define your site's layout. So for example, a simple one page site would have a Web.sitemap file something like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
        <siteMapNode url="~/Default.aspx" title="Page Title" description="Page Description." />
    </siteMap>

    One thing that often confuses people when they are trying to set up their sitemap file is that they think that the structure defined in the file should match the file and folder structure of their web site. While you can set up the navigation to your site in this manner and it might even make sense if your files and folders are well grouped, there's absolutely no reason that you have to do so. The navigation structure that you set up in the sitemap is simply for display purposes. The URL for the deepest node in your sitemap can be in the root of your site and likewise the root node in your sitemap can be buried in your site's deepest folders. Simply put, a file's physical location has no bearing on where the page that that file represents can be located in your sitemap. Even more interesting is the fact that each node doesn't even need to point to a unique file. I'll illustrate the point by presenting the sample Web.sitemap file that I'll be using for the rest of the samples in this article.

    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
      <siteMapNode url="~/Default.aspx" title="Our Site" description="This is the site's root.">
        <siteMapNode url="~/Default.aspx?page=home"     title="Home Page" description="This is the site's home page." />
        <siteMapNode url="~/Default.aspx?page=about"    title="About Us"  description="This page tells the user about us." />
        <siteMapNode url="~/Default.aspx?page=products" title="Products"  description="This page lists the products we sell.">
          <siteMapNode url="~/Default.aspx?page=hardware" title="Hardware" description="This page lists our hardware products." />
          <siteMapNode url="~/Default.aspx?page=software" title="Software" description="This page lists our software products." />
        </siteMapNode>
        <siteMapNode url="~/Plain.aspx" title="Plain Version" description="This is a plain version of the page." />
      </siteMapNode>
    </siteMap>

    Notice how most of the nodes point to the same file and simply pass a different value for the page parameter.

    The Menu Control

    The first control we'll examine is the <asp:Menu> control. Simply put, the menu control lists menu items. These items can come from a number of sources, but in this case, we want to use the entries from our Web.sitemap file. The easiest way to do this is to use an <asp:SiteMapDataSource> control. You define a SiteMapDataSource control like this:

    <asp:SiteMapDataSource id="mySiteMapDataSource" runat="server"
        ShowStartingNode="false"
    />

    I've set the ShowStartingNode property to "false" in order to hide the root node of the sitemap.

    The next order of business is to add a Menu control to the page. At it's most basic, the declaration for a menu control will look like this:

    <asp:Menu id="myMenu" runat="server" DataSourceID="mySiteMapDataSource" />

    With just the Web.sitemap file and those two controls we get a page that looks something like this:

    Now I know it doesn't look like much, but you can format it to your heart's content. For now I'm leaving it simple so as not to confuse anyone.

    The SiteMapPath Control

    The next control I'd like to introduce you to is the <asp:SiteMapPath> control. This control displays what are called breadcrumbs -- a series of links which show the user a path back up the tree to the root of the site. Because the SiteMapPath control is strictly a navigation control and doesn't have any other real functionality, it's even simpler to use then the Menu control. This time around we don't even need to provide a SiteMapDataSource. The control simply uses the default SiteMap provider which reads in the values from your Web.sitemap file automatically.

    Simply add the control to any page which is listed in your sitemap file:

    <asp:SiteMapPath id="mySiteMapPath" runat="server" />

    and the control will automatically display the user's current location along with the route back to the root of the site:

    Once again, I'll leave the formatting to you, but the basic functionality couldn't be easier to implement.

    Please note that the control will not display on a page that is not listed in the sitemap file. After all, how is the control going to know the navigational path back out to the root if it doesn't know where the current file goes.

    The TreeView Control

    The treeview is probably the most logical control for displaying this type of hierarchical data. Anyone who's ever browsed their hard drive in Windows Explorer or visited Microsoft's MSDN site should be familiar with this type of interface. The problem was that they used to be a pain to implement. Well no longer... the <asp:TreeView> does almost all the work for you.

    Just as we did with the Menu control, we'll need to create an <asp:SiteMapDataSource> control.

    <asp:SiteMapDataSource id="mySiteMapDataSource" runat="server"
        ShowStartingNode="false"
    />

    Note: If you're using both the Menu and TreeView on the same page, it's perfectly fine to bind them both to the same SiteMapDataSource if you'd like. There's no need to create two SiteMapDataSource objects unless you want to change the configuration on one of them.

    Setting up the TreeView control is just like the Menu control. Simply add the control to the page and then set the DataSourceID to the name you gave the SiteMapDataSource.

    <asp:TreeView id="myTreeView" runat="server" DataSourceID="mySiteMapDataSource" />

    Once you've done that, if you view the resulting page in a browser you'll see something like this:

    Once again I've left the default formatting just to keep things as simple as possible.

    Putting It All Together and Making It Pretty

    While it's doubtful that you'd actually want to put all three controls on the same page, that's exactly what this next sample does. I'm also going to apply some basic formatting so that you can get a better feel for what these controls might look like if you run across them "in the wild".

    I'm not going to provide the code listing here because all the formatting makes for a relatively long file, but take a look at the screen shot and you'll see that these controls can be made to look like pretty much anything you want them to.

    While it's nothing all that fancy, the formatted version in the zip file will give you an idea of what you can do. I'd recommend you get the controls working the way you want them to first, but once you've got everything working, feel free to play around with the formatting options. The guys at Microsoft really did a good job of making the controls extremely powerful and simple to use, but at the same time allowing you to tweak them to your heart's content.

    Download

    If you'd like a copy of the files used in the samples above, you can download a zip file containing them from here: ASP.NET 2.0 Navigation Controls Sample Code (5.6 KB).

    Conclusion

    I hope this article has shown you just how powerful and easy to use the new ASP.NET 2.0 Navigation controls actually are. Long gone are the days of hand coding all sorts of funky client-side JavaScript just to create a simple menu. Not only does ASP.NET 2.0 do all that work for you, but it does it well and even lets you tweak the display until it looks exactly like you want it to.

    If you're not already using the navigation controls in your web projects ask yourself: "Why not?" Unless you can come up with a very good reason, give them a try. I think you'll be pleasantly surprised.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry