Uredi

Deli z drugimi prek


MenuItemCollection Class

Definition

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

public ref class MenuItemCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class MenuItemCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type MenuItemCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class MenuItemCollection
Implements ICollection, IStateManager
Inheritance
MenuItemCollection
Implements

Examples

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>

<%@ 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="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>

<%@ 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>

Remarks

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.

Constructors

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

Properties

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[Int32]

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.

Methods

Add(MenuItem)

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

AddAt(Int32, MenuItem)

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(MenuItem)

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(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)
GetEnumerator()

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

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(MenuItem)

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

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(MenuItem)

Removes the specified MenuItem object from the MenuItemCollection object.

RemoveAt(Int32)

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)

Explicit Interface Implementations

IStateManager.IsTrackingViewState

Gets a value that indicates whether the MenuItemCollection object is saving changes to its view state.

IStateManager.LoadViewState(Object)

Loads the MenuItemCollection object's previously saved view state.

IStateManager.SaveViewState()

Saves the changes to view state to an Object.

IStateManager.TrackViewState()

Instructs the MenuItemCollection object to track changes to its view state.

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also