Controls You Can Use on Web Forms ASP.NET Navigation Controls Menu Control
The ASP.NET Menu control is a full-featured navigation control that can display both static and dynamic menus in Web Forms pages.
The contents of a Menu control can be set directly in the control, or by binding the control to a data source.
The content, behavior, and appearance of a Menu control can all be decided at design time, using simple declarative syntax.
The control, though, supports programmatic access to the Menu object model, enabling developers to extend the control’s basic functionality, such as to create menus, populate menu items, set properties, and so on, dynamically.
The following sections briefly introduce the main features of the Menu control.
Basically, a Menu control is made up of MenuItem objects that are arranged in one or more levels. Menu levels are specified by nesting parent and child menu items in hierarchical or tree-like fashion.
Menu items at the top level ( level 0 ) are called root menu items. An individual root menu item can have one or more child menu items ( level 1 ), each of which can also be a parent to other child menu items ( level 2 ), and so on.
The following table describes the three different menu item types.
Menu Item Type |
Description |
Root |
A menu item that has no parent and has one or more child menu items. |
Parent |
A menu item that has a parent and has one or more child menu items. |
Child |
A menu item that has a parent but has no child menu items. |
Each menu item has a Text property, which is what displays in the Menu control.
The menu items for a Menu control may be defined using any of the following ways:
- by adding individual MenuItem objects declaratively. For particulars, see Adding Menu Items Declaratively.
- by programmatically creating new instances of the MenuItem class. For particulars, see Adding Menu Items Programmatically.
- by binding the control to a data source. For particulars, see Binding a Menu Control to a Data Source.
How a Menu control responds when a user clicks a menu item depends on the following situations:
- If the NavigateUrl property of a menu item is set, the Menu control navigates to the linked Web page.
By default, a linked page is displayed in the same window or frame as the Menu control. To display the linked content in a different window or frame, use the Target property of the Menu control.
NOTE: The Target property affects every menu item in the control. To specify a window or frame for an individual menu item, set the Target property of the MenuItem object directly.
- If the NavigateUrl property is not set, the Menu control simply posts the page back to the server for processing.
Each menu item has a Value property, which can be used to store any additional data about the menu item, such as data passed to the postback event that is associated with the menu item.
The Menu control supports two types of display modes:
- Static display, in which the Menu control is fully expanded all the time. The entire structure is visible, and a user can click on any part of the menu.
- Dynamic display, in which only specified portions of the menu are static, while their child menu items are displayed when the user holds the mouse pointer ( or hovers ) over the parent menu item.
By default, the menu items at the root level ( level 0 ) are displayed in the static menu.
To control static display behavior, the Menu control exposes a StaticDisplayLevels property, which specifies how many additional menu levels ( static submenus ) to show from the root of the menu. For example, if StaticDisplayLevels is set to 3, the menu will be expanded to statically display its first three levels.
The minimum static display level is 1, in order to show the root menu items. The control will throw an exception if the value is set to 0 or less ( a negative number ).
A dynamic menu appears only when the user positions the mouse pointer over the parent menu item that contains a submenu.
By default, menu items with a level higher than the value specified by the StaticDisplayLevels property are displayed in a dynamic menu.
To control dynamic display behavior, the Menu control exposes a MaximumDynamicDisplayLevels property, which specifies how many additional menu levels should dynamically appear after the static display level. For example, if the menu has a static level of 3 and a max dynamic level of 2, the first three levels of the menu would be statically displayed, while the next two levels would show dynamically.
Menu items with a level higher than the value specified by the MaximumDynamicDisplayLevels property will not be displayed.
The minimum max dynamic display level is 0, meaning no menu menu items will display dynamically. The control will throw an exception if the value is set to less than zero.
Dynamic menus automatically disappear after a certain duration, which can be set by using the DisappearAfter property.
Adding Menu Items Declaratively Adding Menu Items Programmatically