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 type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | MenuItemCollection() | Initializes a new instance of the MenuItemCollection class using the default values. |
![]() | MenuItemCollection(MenuItem) | Initializes a new instance of the MenuItemCollection class using the specified parent menu item (or owner). |
| Name | Description | |
|---|---|---|
![]() | Count | Gets the number of menu items contained in the current MenuItemCollection object. |
![]() | IsSynchronized | Gets a value indicating whether access to the MenuItemCollection object is synchronized (thread safe). |
![]() | Item | Gets the MenuItem object at the specified index in the current MenuItemCollection object. |
![]() | SyncRoot | Gets an object that can be used to synchronize access to the MenuItemCollection object. |
| Name | Description | |
|---|---|---|
![]() | Add | Appends the specified MenuItem object to the end of the current MenuItemCollection object. |
![]() | AddAt | Inserts the specified MenuItem object in the current MenuItemCollection object at the specified index location. |
![]() | Clear | Removes all items from the current MenuItemCollection object. |
![]() | Contains | Determines whether the specified MenuItem object is in the collection. |
![]() | CopyTo(Array, Int32) | Copies all the items from the MenuItemCollection object to a compatible one-dimensional Array, starting at the specified index in the target array. |
![]() | CopyTo(array<MenuItem>, Int32) | Copies all the items from the MenuItemCollection object to a compatible one-dimensional array of MenuItem objects, starting at the specified index in the target array. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetEnumerator | Returns an enumerator that can be used to iterate through the items in the current MenuItemCollection object. |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | IndexOf | Determines the index of the specified MenuItem object in the collection. |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | Remove | Removes the specified MenuItem object from the MenuItemCollection object. |
![]() | RemoveAt | Removes the MenuItem object at the specified index location from the current MenuItemCollection object. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() | AsParallel | Enables parallelization of a query. (Defined by ParallelEnumerable.) |
![]() | AsQueryable | Converts an IEnumerable to an IQueryable. (Defined by Queryable.) |
![]() | Cast<TResult> | Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.) |
![]() | OfType<TResult> | Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.) |
| Name | Description | |
|---|---|---|
![]() ![]() | IStateManager::IsTrackingViewState | Infrastructure. Gets a value that indicates whether the MenuItemCollection object is saving changes to its view state. |
![]() ![]() | IStateManager::LoadViewState | Infrastructure. Loads the MenuItemCollection object's previously saved view state. |
![]() ![]() | IStateManager::SaveViewState | Infrastructure. Saves the changes to view state to an Object. |
![]() ![]() | IStateManager::TrackViewState | Infrastructure. Instructs the MenuItemCollection object to track changes to its view state. |
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.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.







Note