MenuItemCollection Class
Represents a collection of menu items in a Menu control. This class cannot be inherited.
Assembly: System.Web (in System.Web.dll)
The MenuItemCollection class is used to store and manage a collection of MenuItem objects in the Menu control. The Menu control uses the MenuItemCollection class to store its root menu items in the Items property. This collection is also used for the ChildItems property of a MenuItem object to store a menu item's submenu items (if any).
The MenuItemCollection class supports several ways to access the items in the collection:
Use the Item indexer to directly retrieve a MenuItem object at a specific zero-based index.
Use the GetEnumerator method to create an enumerator that can be used to iterate through the collection.
Use the CopyTo method to copy the contents of the collection into an array.
You can programmatically manage a MenuItemCollection object by adding and removing MenuItem objects. To add menu items to the collection, use the Add or the AddAt method. To remove nodes from the collection, use the Remove, the RemoveAt, or the Clear method.
Note: |
|---|
When the Menu control is bound to a data source, the Items and ChildItems collections are automatically populated each time binding occurs. Any changes to the collections between bindings will be lost. To retain these changes, either update the data source or manually rebuild the collection each time you bind. |
The MenuItemCollection class contains properties and methods that allow you to retrieve information about the collection itself. To find out how many items are in the collection, use the Count property. If you want to determine whether the collection contains a certain MenuItem object, use the Contains method. To get the index of a MenuItem object in the collection, use the IndexOf method.
| Topic | Location |
|---|---|
| Walkthrough: Displaying a Menu on Web Pages | Building ASP .NET Web Applications in Visual Studio |
| Walkthrough: Controlling ASP.NET Menus Programmatically | Building ASP .NET Web Applications in Visual Studio |
The following code example demonstrates how to populate the Items and ChildItems collections using declarative syntax.
The following code example demonstrates how to programmatically add a MenuItem object to the ChildItems collection of a root menu item.
- AspNetHostingPermission
for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission
for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: