nless your Web site contains only a single Web page, you need a mechanism for visitors to navigate between pages. For most sites, navigation is right up there with content in term of its importance to your site's success. And there are several different ways of coding one's navigation. But ASP.NET 2.0, with several new navigation controls, introduces perhaps the easiest way to date.
These new controls are:
Creating the Site Map
To see how to implement the controls in ASP.NET, create a new Web site project in Visual Studio 2005. In Solution Explorer, add a site map by right-clicking on the project name and selecting Add New Item -> Site Map. A site map is an XML document describing the logical structure of a Web site.
Listing 1 contains some sample code to populate the site map. Its primary structure is a series of <siteMapNode> elements.
There are four attributes to each <siteMapNode> element in Listing 1:
![]() Figure 1. Populating the Master Page: Putting a SiteMapPath control on the master page makes it available on all pages that inherit from the master page. |
![]() Figure 2. Location, Location: Expand the Navigation menu to expose the SiteMapPath and Menu controls. |
![]() | |
| Figure 3. Choose Your Master: Be sure to check the box for "Select Master Page" for your Web form. |
Now add another new Web Form, using the same process as before and name it Windowsmobileoverview.aspx. Again, populate the form, roughly, with a text heading and a graphic (see Figure 5).
![]() Figure 4. The Main.aspx Page: Put something simple on your main.aspx page. In this case I've used a header and a graphic. |
![]() Figure 5. The Windowsmobileoverview.aspx Page: This page will be a lower page in the site hierarchy for my test site. |
![]() | |
| Figure 6. Testing the SiteMapPath control: In this image, you can see the "bread crumb" function working, as each page shows its place in the navigation hierarchy. |
If you need to change the default name of the site map file, you can do so via the Web.config file. The code below shows how to deregister the default XmlSiteMapProvider and register the same one with a different site map name, in this case, My.sitemap.
<configuration>
<system.web>
<siteMap>
<providers>
<remove name="AspNetXmlSiteMapProvider" />
<add name="AspNetXmlSiteMapProvider"
type="System.Web.XmlSiteMapProvider,
System.Web, Version=1.2.3400.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
siteMapFile="My.sitemap" />
</providers>
</siteMap>
</system.web>
</configuration>
Customizing the SiteMapPath Control
The default view of the SiteMapPath control displays the navigation path using the string contained in the title attribute of the <siteMapNode> element in the site map file (Listing 1). Each navigation path is separated by the ">" character.
![]() | |
| Figure 7. Enhanced Bread Crumb: Customizing the look and feel of the SiteMapPath control. |
<asp:SiteMapPath ID="SiteMapPath2" Font-Name="Garamond"
Font-Size="12pt" RunAt="server">
<CurrentNodeStyle Height="24px" BackColor="LightGreen"
Font-Bold="true" />
<NodeStyle Height="24px" />
<PathSeparatorTemplate>
<ItemTemplate>
<asp:Image ID="Image2"
ImageUrl="~/images/arrow.gif"
RunAt="server" />
</ItemTemplate>
</PathSeparatorTemplate>
</asp:SiteMapPath>
Figure 7 shows what the SiteMapPath control looks like with the above modification.
TreeView and Menu Controls
The SiteMapPath control is just one of several controls in ASP.NET 2.0 that can display site navigation information. In this part of the article, I'll discuss the TreeView and Menu controls.
The TreeView control is best used in very large or very deep sites, where users may frequently need to move quickly among top-level and low-level pages. Unlike static or bread crumb navigation, it allows the user to manipulate the navigation view. In this example, we are adding a TreeView to the existing SiteMapPath control so that both the tree and the bread crumb are used.
![]() | |
| Figure 8. TreeView: Add a new data source to the right side of the table that informs the TreeView control on the left of the Master Page. |
To build a TreeView, begin by adding a 1x2 table to the Master Page by going to Layout -> Insert Table. Add a TreeView control to the left cell of the table and move the ContentPlaceHolder control into the right cell (see Figure 8). In the Smart Tag of the TreeView control, select "<New data source…>" and select Site Map.
For the sake of this sample, I'll just apply the MSDN autoformat (to find it, select Autoformat on the Smart Tag of the TreeView control) to the TreeView control and I'll set the BackColor property of the TreeView control to Gray (#E0E0E0). The Master Page should look like Figure 9. Note that a SiteMapDataSource control is automatically added to the page (because you selected Site Map as the new data source earlier). The SiteMapDataSource control will automatically map to the Web.sitemap file.
Press Ctrl-F5 to compile your work and see the TreeView control in action. You should now be able to navigate among all the pages in your site by selecting the items in the TreeView control (see Figure 10).
![]() Figure 9. The TreeView Control: Using the TreeView control and the MSDN autoformat, you can mimic the menu of MSDN as a test. |
![]() Figure 10. Testing the TreeView Control: The TreeView pulls index information from the same file as the SiteMapPath (bread crumb) control, but it allows the user to manipulate the menu and navigate a deep site more efficiently. |
The Menu control is found in the Standard tab of the Toolbox. Add one to the Master Page and apply the "Colorful" autoformat to change its look (select Autoformat in the Smart Tag of the Menu control). In the Smart Tag of the Menu control, select SiteMapDataSource1 as its data source so that its content can be obtained from the SiteMapDataSource1 control (see Figure 11). The SiteMapDataSource1 control was added in automatically when the TreeView control was configured and contains the site map information (Web.sitemap) given in Listing 1.
To test the Menu control, press Ctrl-F5. You should now be able to navigate the site using the Menu control (see Figure 12).
![]() Figure 11. Adding a Menu Control to the Master Page: You need to select a data source for the Menu control. |
![]() Figure 12. Testing the Menu Control: The Menu control adds "fly out" style navigation to your site. |
Accessing Site Map Information Programmatically
If you are not using a SiteMapPath control, you can still programmatically access site map information through the Site Map API (found in the System.Web.SiteMap namespace). The following code segment shows how to display site navigation information programmatically:
![]() | |
| Figure 13. Changing Orientation: In this screen shot the orientation of the Menu control has been changed to vertical. Compare with the horizontal orientation in Figure 12 to see the difference. |
Partial Class MasterPage_master
Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
Dim myCurrentNode As SiteMapNode
'---Gets the current node
myCurrentNode = SiteMap.CurrentNode
'---displays the navigational path---
Dim path As String = myCurrentNode.Title
While myCurrentNode.ParentNode IsNot Nothing
myCurrentNode = myCurrentNode.ParentNode
path = " > " & path
path = myCurrentNode.Title & path
End While
Response.Write(path)
End Sub
End Class
Besides the CurrentNode property, you can also access site information using:
| DevX is a division of Internet.com. © Copyright 2010 Internet.com. All Rights Reserved. Legal Notices |