Compartir a través de


Walkthrough: Displaying a Menu on Web Pages

This walkthrough illustrates how to place and configure an ASP.NET Menu control on a Web page.

One common aspect of Web sites of any complexity is a navigational menu. You can use the Menu control in ASP.NET to easily set up a complex navigational menu without writing code.

The Menu control allows for multiple display options, including a static display where the menu is fully exposed and a dynamic display where portions of the menu appear as the mouse pointer moves over the parent menu item. The control also provides a combination of static and dynamic display modes that allow a series of root items that are static, but with child menu items that appear dynamically.

You can configure the ASP.NET Menu control in the designer with static links to your pages or you can bind it automatically to a hierarchical data source such as an XmlDataSource or a SiteMapDataSource control.

Tasks illustrated in this walkthrough include:

  • Creating a basic menu and configuring it statically to link to your pages.

  • Creating a more complex menu that is bound to a Web.sitemap XML file.

  • Adjusting the orientation of a menu.

  • Configuring multiple levels of static display versus dynamic display.

Prerequisites

In order to complete this walkthrough, you will need:

  • Visual Studio or Visual Web Developer Express installed on your computer.

    Note

    If you are using Visual Studio, the walkthrough assumes that you selected the Web Development collection of settings when you started Visual Studio the first time. For more information, see How to: Select Web Development Environment Settings.

Creating the Web Site

For this walkthrough, you will create an empty Web site project and add a page to it.

This walkthrough uses a Web site project. You could use a Web application project instead. For information about the difference between these Web project types, see Web Application Projects versus Web Site Projects in Visual Studio.

To create a file system Web site

  1. Open Visual Studio or Visual Web Developer Express.

  2. In the File menu, click New Web Site.

    The New Web Site dialog box is displayed.

  3. Under Installed Templates, click Visual Basic or Visual C# and then select ASP.NET Empty Web Site.

  4. In the Web location box, select File System, and then enter the name of the folder where you want to keep the pages for your Web site.

    For example, type the folder name C:\WebSites.

  5. Click OK.

    Visual Studio creates a Web site project that includes a Web.config file.

  6. In Solution Explorer, right-click the root of your Web site, and then click Add New Item.

  7. Select Web Form, name the file Default.aspx, and then click Add.

Creating a Basic Menu

Although the master page within the ASP.NET Web site template includes a menu, you can add an additional menu specifically for the Default.aspx page for the purpose of this walkthrough. The first step in creating a menu for your page is the placement of a Menu control.

To add a Menu control to the page

  1. Switch to or open Default.aspx, and then switch to Design view.

  2. From the Navigation control group in the Toolbox, drag a Menu control onto the page.

For this example, you will orient the menu horizontally rather than vertically.

To position the Menu control horizontally

  • Right-click the Menu control, click Properties, and then set Orientation to Horizontal.

Configuring a Basic Menu

In this section, you will define the menu items by using the Menu Item Editor.

To edit Menu control items

  1. Right-click the Menu control, and then click Edit Menu Items.

    The Menu Item Editor appears.

  2. Under Items, click the Add a root item icon.

  3. Under Properties for the new item, set Text to Home and NavigateURL to Default.aspx.

  4. Under Items, click the Add a root item icon.

  5. Under Properties, set Text to Products and NavigateURL to Products.aspx.

  6. Under Items, click the Add a root item icon.

  7. Under Properties, set Text to Services and NavigateURL to Services.aspx.

  8. Click OK.

If you look at the source for Default.aspx, you will see that the menu items and links are stated declaratively in the control.

To create the target pages

  1. In Solution Explorer, right-click the root of your Web site, and then click Add New Item.

  2. Click Web Form, name the file Products.aspx, and then click Add.

  3. Repeat the preceding steps and create a file named Services.aspx.

Testing the Menu

With your pages and menu in place, you can try it out.

To test the Menu control

  1. Click Default.aspx in Solution Explorer and then press CTRL+F5 to run the Default.aspx page.

  2. Move the pointer over the items; the browser's status bar at the bottom of the page (if visible) will show what page is linked.

  3. Click a link to jump to the page.

Creating a Menu Bound to a Site Map

In the last section, you created a simple static menu configured declaratively within the page. In this section, you skip editing the Menu control items directly, and instead bind the control to a Web.sitemap file as an XML data source. This allows the Menu control's structure to be maintained within a separate XML file that can be easily updated without modifying the page or using the designer.

For this example you will use a second Menu control.

To create a second Menu control

  • From the Navigation group of the Toolbox, drag a second Menu control onto the Default.aspx page.

Next, you will need a Web.sitemap file to bind to.

To create a site map file

  1. In Solution Explorer, right-click the root of your Web site, and then click Add New Item.

  2. In the Add New Item dialog box, click Site Map.

  3. Click Add.

  4. Place the following XML code in the Web.sitemap file.

    The XML represents the structure of the menu. Nested nodes become child menu items of parent node menu items.

    <siteMap>
      <siteMapNode title="Home" description="Home" url="default.aspx" >
        <siteMapNode title="Products" description="Our products" 
         url="Products.aspx">
          <siteMapNode title="Hardware" 
           description="Hardware choices"
           url="Hardware.aspx" />
          <siteMapNode title="Software" 
           description="Software choices" 
           url="Software.aspx" />
        </siteMapNode>
        <siteMapNode title="Services" 
         description="Services we offer" 
         url="Services.aspx">
          <siteMapNode title="Training" 
           description="Training classes" 
           url="Training.aspx" />
          <siteMapNode title="Consulting" 
           description="Consulting services" 
           url="Consulting.aspx" />
          <siteMapNode title="Support" 
           description="Support plans" 
           url="Support.aspx" />
        </siteMapNode>
      </siteMapNode>
    </siteMap>
    
  5. Save the file.

Binding to a Site Map

Now you can create a navigation data source that points to your Web.sitemap file and bind your Menu control to it.

To bind to a Menu control to a site map

  1. Open the Default.aspx file, and then switch to Design view.

  2. Click the smart tag of the new Menu control to display the Menu Tasks dialog box.

  3. In the Menu Tasks dialog box, in the Choose Data Source drop-down list, click New Data Source.

    The Data Source Configuration Wizard dialog box appears.

  4. Click Site Map.

    Under Specify an ID for the data source, the default name, SiteMapDataSource1, appears.

  5. Click OK.

Testing Site Map Binding

With your pages and menu in place, you can try it out.

To test the site map binding

  1. Press CTRL+F5 to run the Default.aspx page.

  2. Move the pointer over the Home menu item of the second, vertical menu.

    Products and Services appear.

  3. Move the pointer over Products.

    Hardware and Software appear.

    If you look at the source code for Default.aspx, you will notice that unlike the first menu item, the items are not specified declaratively; instead, the data source is referenced by the Menu control.

  4. Close the Browser window.

Adjusting Static and Dynamic Levels

The Menu control has two modes of display: static and dynamic. Static display means that the Menu control is fully expanded all the time. The entire structure is visible, and a user can click on any part. In a dynamically displayed menu, only the portions you specify are static, while their child menu items are displayed when the user holds the mouse pointer over the parent node.

The vertical menu you created in the preceding section uses a fully dynamic display, with only the top level remaining static. With the Menu control, you have the ability to specify behavior based on pausing the mouse pointer and whether or not the menu should be dynamic or static. In this section, you will adjust the dynamic and static qualities of the Menu control.

To make the Menu control static for two levels

  1. On the Default.aspx page in Design view, right-click the second Menu control, and then click Properties.

  2. Set StaticDisplayLevels to 2.

Testing the Dynamic Menu

With your pages and menu in place, you can try it out.

To test the dynamic menu

  • Press CTRL+F5 to run the Default.aspx page.

    The first two levels of the menu are displayed, whereas the third level is dynamic.

Next Steps

The Menu control allows you to easily create navigation menus. You can configure the control for dynamic or static display and bind it to a site map XML file. You might also want to experiment with the following additional options:

See Also

Tasks

Walkthrough: Controlling ASP.NET Menus Programmatically

Concepts

Menu Control Overview