|
||||||||||||||||||||||
This two-part series explores nine of the most common site map problems and shows how to solve them with ASP.NET 2.0's new navigation features. Part 1 demonstrates how to:
The ASP.NET 2.0 solution to site navigation is extremely easy to set up. Initial configuration takes less than a minute. If you are starting with a blank solution add a few pages such as Default.aspx and SiteMap.aspx. Then, in Visual Studio 2005 add a new item of type "Site Map" named Web.sitemap. Inside your new Web.sitemap XML file, add siteMapNode elements for each of your pages. Specify the locations and titles for your pages using the url and title attributes. Here's a sample snippet of a Web.sitemap file: Next you must tell ASP.NET to use the Web.sitemap file. Before the closing element in your Web.config file, add: To see how this works add the following to Default.aspx or your master page: When you run the application, the title of the Default.aspx page displays as "Home!" #1: Making Buckets Pretty The most common site navigation problem developers face is displaying a site's buckets (top level pages). Displaying buckets as images is slightly more interesting than displaying them as plain text, because you must store the image's filename, and perhaps its size. This first solution shows how to display top-tier images as a set of "tabs" by binding a Repeater control to a SiteMapDataSource control and using the IsDescendantOf() method of ASP.NET 2.0's new System.Web.SiteMap class. Figure 1 shows the final result for a Site Map page where each bucket is represented by an image.
The SiteMapDataSource control provides easy programmatic access to the hierarchical site map information contained in Web.sitemap. Of all SiteMapDataSource's properties, ShowStartingNode is the most useful. Setting it to false removes the mandatory one-and-only-one top-level siteMapNode in Web.sitemap, and exposes its children (the buckets). StartingNodeOffset is the next most useful property because it lets you move up and down the site map navigational tree. I'll explain this topic in greater detail in Solution #4. The most common properties of SiteMapDataSource are described in Table 1. Table 1: Commonly SiteMapDataSource Properties: The table shows the most commonly-used SiteMapDataSource properties and a description of each.
A TreeView is one of the few controls that can display all a SiteMapDataSource's pages, because it can handle hierarchical information natively. The repeater used above, however, typically does not. What it will do is display the topmost pages exposed by the SiteMapDataSource. This is why SiteMapDataSource's ShowStartingNode property is so useful. By binding to the SiteMapDataSource through the DataSourceID property, the repeater can access the siteMapNode elements contained in the SiteMapDataSource. Each SiteMapNode object corresponds to one of the entries in the Web.sitemap file (see earlier example). The repeater may then access the siteMapNode element's attributes, such as url and title. But what about storing and retrieving other attributes, such as imageName? What makes ASP.NET 2.0's approach to site navigation so wonderful is its extensibility. You can add arbitrary attributes to the siteMapNode elements in the Web.sitemap file, and then access them in code. The solution above adds the imageName attribute to the Web.sitemap file as shown below: Then, you can access the value in the RepeaterItemTemplate: You could easily add image height and width attributes as well. That covers the basics of displaying image-oriented navigation. The next sections expand on this approach by showing how to identify active buckets.
|
||||||||||||||||||||||
|