Export (0) Print
Expand All

MenuItemCollection Class

Represents a collection of menu items in a Menu control. This class cannot be inherited.

System.Object
  System.Web.UI.WebControls.MenuItemCollection

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public sealed class MenuItemCollection : ICollection, 
	IEnumerable, IStateManager

The MenuItemCollection type exposes the following members.

  NameDescription
Public methodMenuItemCollection()Initializes a new instance of the MenuItemCollection class using the default values.
Public methodMenuItemCollection(MenuItem)Initializes a new instance of the MenuItemCollection class using the specified parent menu item (or owner).
Top

  NameDescription
Public propertyCountGets the number of menu items contained in the current MenuItemCollection object.
Public propertyIsSynchronizedGets a value indicating whether access to the MenuItemCollection object is synchronized (thread safe).
Public propertyItemGets the MenuItem object at the specified index in the current MenuItemCollection object.
Public propertySyncRootGets an object that can be used to synchronize access to the MenuItemCollection object.
Top

  NameDescription
Public methodAddAppends the specified MenuItem object to the end of the current MenuItemCollection object.
Public methodAddAtInserts the specified MenuItem object in the current MenuItemCollection object at the specified index location.
Public methodClearRemoves all items from the current MenuItemCollection object.
Public methodContainsDetermines whether the specified MenuItem object is in the collection.
Public methodCopyTo(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.
Public methodCopyTo(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.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that can be used to iterate through the items in the current MenuItemCollection object.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfDetermines the index of the specified MenuItem object in the collection.
Public methodRemoveRemoves the specified MenuItem object from the MenuItemCollection object.
Public methodRemoveAtRemoves the MenuItem object at the specified index location from the current MenuItemCollection object.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate propertyIStateManager.IsTrackingViewStateInfrastructure. Gets a value that indicates whether the MenuItemCollection object is saving changes to its view state.
Explicit interface implemetationPrivate methodIStateManager.LoadViewStateInfrastructure. Loads the MenuItemCollection object's previously saved view state.
Explicit interface implemetationPrivate methodIStateManager.SaveViewStateInfrastructure. Saves the changes to view state to an Object.
Explicit interface implemetationPrivate methodIStateManager.TrackViewStateInfrastructure. Instructs the MenuItemCollection object to track changes to its view state.
Top

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.

NoteNote

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.

TopicLocation
Walkthrough: Controlling ASP.NET Menus ProgrammaticallyBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Displaying a Menu on Web PagesBuilding ASP .NET Web Applications in Visual Studio

The following code example demonstrates how to populate the Items and ChildItems collections using declarative syntax.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

  <!-- For the hover styles of the Menu control to  -->
  <!-- work correctly, you must include this head   -->
  <!-- element.                                     -->
  <head runat="server">
    <title>Menu Declarative Example</title>
</head>

  <body>
    <form id="form1" runat="server">

      <h3>Menu Declarative Example</h3>

      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Submenu items are      -->
      <!-- created by nesting them in parent menu -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"  
        runat="server">

        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>

        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>

      </asp:menu>

    </form>
  </body>
</html>

The following code example demonstrates how to programmatically add a MenuItem object to the ChildItems collection of a root menu item.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      // Retrieve the root menu item from the Items
      // collection of the Menu control using the indexer.
      MenuItem homeMenuItem = NavigationMenu.Items[0];

      // Create the submenu item.
      MenuItem newSubMenuItem = new MenuItem("New Category");

      // Add the submenu item to the ChildItems
      // collection of the root menu item.
      homeMenuItem.ChildItems.Add(newSubMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection Add Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>MenuItemCollection Add Example</h3>

      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server">

        <items>
          <asp:menuitem text="Home"
            tooltip="Home">
            <asp:menuitem text="Music"
              tooltip="Music">
              <asp:menuitem text="Classical"
                tooltip="Classical"/>
              <asp:menuitem text="Rock"
                tooltip="Rock"/>
              <asp:menuitem text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem text="Movies"
              tooltip="Movies">
              <asp:menuitem text="Action"
                tooltip="Action"/>
              <asp:menuitem text="Drama"
                tooltip="Drama"/>
              <asp:menuitem text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>

      </asp:menu>

    </form>
  </body>
</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft