21.8. Dynamic Menus

Problem

You want to use a dynamic menu in your application to provide a compact means for a user to navigate within your application, and you want to accomplish this without using a third-party control.

Solution

Create a Web.sitemap file to define the pages and navigation in your application. Create a .master file containing a SiteMapDataSource control and a Menu control with the DataSourceID set to the ID of the SiteMapDataSource control and other HTML common to all pages in your application. Use the .master file as the master page for all pages in your application that require the dynamic menu.

In the Web.sitemap file:

  1. Add a siteMapNode element for each section of your application.

  2. Add a siteMapNode element for each page in the sections.

In the .master file:

  1. Add a SiteMapDataSource control.

  2. Add a Menu control.

  3. Set the DataSourceID attribute of the Menu control to the value of the ID attribute of the SiteMapDataSource control.

In the .aspx files that require the dynamic menu, set the MasterPageFile attribute of the @ Page directive to the name of the .master file.

Example 21-21 shows the Web.sitemap file, Example 21-22 shows the .master file, and Example 21-23 shows the .aspx file for this example. Figure 21-7 shows the output of the test page with the dynamic menu in the static state. Figure 21-8 shows the output with the dynamic menu expanded.

Dynamic menu in static state

Figure 21-7. Dynamic menu in ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.