MenuItemCollection Class
TOC
Collapse the table of content
Expand the table of content

MenuItemCollection Class

 

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

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

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

Public NotInheritable Class MenuItemCollection
	Implements ICollection, IEnumerable, IStateManager

NameDescription
System_CAPS_pubmethodMenuItemCollection()

Initializes a new instance of the MenuItemCollection class using the default values.

System_CAPS_pubmethodMenuItemCollection(MenuItem)

Initializes a new instance of the MenuItemCollection class using the specified parent menu item (or owner).

NameDescription
System_CAPS_pubpropertyCount

Gets the number of menu items contained in the current MenuItemCollection object.

System_CAPS_pubpropertyIsSynchronized

Gets a value indicating whether access to the MenuItemCollection object is synchronized (thread safe).

System_CAPS_pubpropertyItem(Int32)

Gets the MenuItem object at the specified index in the current MenuItemCollection object.

System_CAPS_pubpropertySyncRoot

Gets an object that can be used to synchronize access to the MenuItemCollection object.

NameDescription
System_CAPS_pubmethodAdd(MenuItem)

Appends the specified MenuItem object to the end of the current MenuItemCollection object.

System_CAPS_pubmethodAddAt(Int32, MenuItem)

Inserts the specified MenuItem object in the current MenuItemCollection object at the specified index location.

System_CAPS_pubmethodClear()

Removes all items from the current MenuItemCollection object.

System_CAPS_pubmethodContains(MenuItem)

Determines whether the specified MenuItem object is in the collection.

System_CAPS_pubmethodCopyTo(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.

System_CAPS_pubmethodCopyTo(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.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that can be used to iterate through the items in the current MenuItemCollection object.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIndexOf(MenuItem)

Determines the index of the specified MenuItem object in the collection.

System_CAPS_pubmethodRemove(MenuItem)

Removes the specified MenuItem object from the MenuItemCollection object.

System_CAPS_pubmethodRemoveAt(Int32)

Removes the MenuItem object at the specified index location from the current MenuItemCollection object.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.LoadViewState(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Loads the MenuItemCollection object's previously saved view state.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.SaveViewState()

This API supports the product infrastructure and is not intended to be used directly from your code. Saves the changes to view state to an Object.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.TrackViewState()

This API supports the product infrastructure and is not intended to be used directly from your code. Instructs the MenuItemCollection object to track changes to its view state.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIStateManager.IsTrackingViewState

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value that indicates whether the MenuItemCollection object is saving changes to its view state.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast(Of TResult)()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType(Of TResult)()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

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.

System_CAPS_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.

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


<%@ Page Language="VB" %>

<!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="VB" %>

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

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then

      ' Retrieve the root menu item from the Items
      ' collection of the Menu control using the indexer.
      Dim homeMenuItem As MenuItem = NavigationMenu.Items(0)

      ' Create the submenu item.
      Dim newSubMenuItem = New MenuItem("New Category")

      ' Add the submenu item to the ChildItems
      ' collection of the root menu item.
      homeMenuItem.ChildItems.Add(newSubMenuItem)

    End If

  End Sub

</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
Available since 2.0

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

Return to top
Show:
© 2016 Microsoft