System.Web.UI.WebControls.MenuItem Class

Represents a menu item displayed in the System.Web.UI.WebControls.Menu control. This class cannot be inherited.

See Also: MenuItem Members

Syntax

[System.Web.UI.ParseChildren(true, "ChildItems")]
public sealed class MenuItem : ICloneable, System.Web.UI.IStateManager

Remarks

A System.Web.UI.WebControls.Menu control is made up of a hierarchy of menu items represented by System.Web.UI.WebControls.MenuItem objects. Each menu item has a read-only MenuItem.Depth property that specifies the level at which the menu item is displayed in the System.Web.UI.WebControls.Menu control. Menu items at the top level (level 0) that do not have a parent menu item are called root menu items. A menu item that has a parent menu item is called a submenu item. All root menu items are stored in the Menu.Items collection. Submenu items are stored in a parent menu item's MenuItem.ChildItems collection. You can access a menu item's parent menu item by using the MenuItem.Parent property.

Note:

The Menu.Items and MenuItem.ChildItems collections contain only the menu items for the next level down. To access menu items further down the menu tree, use the MenuItem.ChildItems property of a subsequent menu item.

To create the menu items for a System.Web.UI.WebControls.Menu control, use one of the following methods:

Use declarative syntax to create static menu items.

Use a constructor to dynamically create new instances of the System.Web.UI.WebControls.MenuItem class. These System.Web.UI.WebControls.MenuItem objects can then be added to the Menu.Items or MenuItem.ChildItems collection.

Bind the System.Web.UI.WebControls.Menu control to a data source. When the System.Web.UI.WebControls.Menu control is bound to a System.Web.UI.WebControls.SiteMapDataSource control, System.Web.UI.WebControls.MenuItem objects are automatically created that correspond to the items in the data source. For other data sources, System.Web.UI.WebControls.MenuItem objects are also automatically created that match the hierarchy structure of data source; however, you must also use the Menu.DataBindings collection to define the menu item bindings that specify the binding relationship between a menu item and its corresponding data item.

The menu items are displayed in either a static menu or a dynamic menu, depending on its level. The static menu is always displayed in a System.Web.UI.WebControls.Menu control. By default, the menu items at the top level (level 0) are displayed in the static menu. You can display additional menu levels (static submenus) within the static menu by setting the Menu.StaticDisplayLevels property. Menu items (if any) with a higher level than the value specified by the Menu.StaticDisplayLevels property are displayed in a dynamic submenu. A dynamic submenu appears only when the user positions the mouse pointer over the parent menu item that contains a dynamic submenu.

When the user clicks a menu item, the System.Web.UI.WebControls.Menu control can either navigate to a linked Web page or simply post back to the server. If the MenuItem.NavigateUrl property of a menu item is set, the System.Web.UI.WebControls.Menu control navigates to the linked page; otherwise, it posts the page back to the server for processing. By default, a linked page is displayed in the same window or frame as the System.Web.UI.WebControls.Menu control. To display the linked content in a different window or frame, use the Menu.Target property of the System.Web.UI.WebControls.Menu control.

Note:

The Menu.Target property affects every menu item in the control. To specify a window or frame for an individual menu item, set the MenuItem.Target property of the System.Web.UI.WebControls.MenuItem object directly.

Each menu item has a MenuItem.Text and a MenuItem.Value property. The value of the MenuItem.Text property is displayed in the System.Web.UI.WebControls.Menu control, while the MenuItem.Value property is used to store any additional data about the menu item, such as data passed to the postback event associated with the menu item. If you set the MenuItem.Text property, but leave the MenuItem.Value property unset, the MenuItem.Value property is automatically set with the same value as the MenuItem.Text property. The opposite is also true. If you set the MenuItem.Value property, but not the MenuItem.Text property, the MenuItem.Text property is automatically set with the same value as the MenuItem.Value property.

Note:

Menu items at the same menu level must each have a unique value for the MenuItem.Value property; the System.Web.UI.WebControls.Menu control cannot distinguish between different menu items at the same level that have the same value. In this scenario, if the user clicks a menu item that has a duplicate value, the menu item that appears first in the menu is selected.

To display a ToolTip when the user positions the mouse pointer over a menu item, set the item's MenuItem.ToolTip property.

A System.Web.UI.WebControls.Menu control has several different types of menu items. You can control the style (such as font size and color) for the different menu item types by using the properties in the following table.

Menu.DynamicHoverStyle

The style settings for a dynamic menu item when the mouse pointer is positioned over it.

Menu.DynamicMenuItemStyle

The style settings for an individual dynamic menu item.

Menu.DynamicMenuStyle

The style settings for a dynamic menu.

Menu.DynamicSelectedStyle

The style settings for the currently selected dynamic menu item.

Menu.StaticHoverStyle

The style settings for a static menu item when the mouse pointer is positioned over it.

Menu.StaticMenuItemStyle

The style settings for an individual static menu item.

Menu.StaticMenuStyle

The style settings for a static menu.

Menu.StaticSelectedStyle

The style settings for the currently selected static menu item.

Instead of setting the individual style properties, you can specify styles that are applied to menu items based on their level by using the style collections shown in the following table.

Menu.LevelMenuItemStyles

A collection of System.Web.UI.WebControls.MenuItemStyle objects that control the style of the menu items based on their level.

Menu.LevelSelectedStyles

A collection of System.Web.UI.WebControls.MenuItemStyle objects that control the style of selected menu items based on their level.

Menu.LevelSubMenuStyles

A collection of System.Web.UI.WebControls.MenuItemStyle objects that control the style of the submenu items based on their level.

The first style in the collection corresponds to the style of the menu items at the first depth level in the menu tree. The second style in the collection corresponds to the style of the menu items at the second depth level in the menu tree, and so on. This is most often used to generate table of contents-style navigation menus where menu items at a certain depth should have the same appearance, regardless of whether they have submenus.

Note:

If you use any of the level style collections listed in the previous table to define the style for the System.Web.UI.WebControls.Menu control, these style settings override the individual menu item style properties.

In addition to customizing a menu item's style, you can also customize its appearance. You can specify custom images for the different parts for a menu item by setting the properties in the following table.

MenuItem.ImageUrl

An optional image displayed next to the text of a menu item.

MenuItem.PopOutImageUrl

An optional image displayed in a menu item to indicate that the menu item has a dynamic submenu.

MenuItem.SeparatorImageUrl

An optional image displayed at the bottom of a menu item to separate it from other menu items.

To determine whether a menu item is currently selected in a System.Web.UI.WebControls.Menu control, use the MenuItem.Selected property. You can also determine whether a menu item is bound to data by using the MenuItem.DataBound property. If a menu item is bound to data, you can use the MenuItem.DataItem property to access the values of the data item bound to the menu item.

For a list of initial property values for an instance of the System.Web.UI.WebControls.MenuItem class, see the MenuItem.#ctor constructor.

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0