This topic has not yet been rated - Rate this topic

MenuItemCollection.GetEnumerator Method

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

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

public IEnumerator GetEnumerator ()
public final IEnumerator GetEnumerator ()
public final function GetEnumerator () : IEnumerator
Not applicable.

Return Value

An enumerator that can be used to iterate through the items in the current MenuItemCollection.

Use the GetEnumerator method to create an enumerator that can be easily iterated through to get each item in the current MenuItemCollection object. To get the item currently pointed to in the enumerator, use the Current property. Use the MoveNext method to move to the next item. If you need to move the enumerator back to the beginning of the collection, use the Reset method.

NoteNote:

After you create an enumerator or use the Reset method, you must call the MoveNext method. Otherwise, the item represented by the Current property is undefined.

As an alternative, you can also use the CopyTo method to copy the items in the collection to an array. You can then use the array to access the items in the collection.

The following code example demonstrates how to use the GetEnumerator method to create an enumerator that contains the submenu items of the Music menu item in a Menu control.


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

    // Display the submenu items of the Music
    // menu item. 

    // Retrieve the Music menu item.
    MenuItem musicMenuItem = NavigationMenu.FindItem(@"Home\Music");

    // Use the GetEnumerator method to create an enumerator 
    // that contains the submenu items of the Music menu item.
    IEnumerator menuItemEnumerator = musicMenuItem.ChildItems.GetEnumerator();

    Message.Text = "The submenu items of the Music menu item are: <br/><br/>";

    // Iterate though the enumerator to display the menu items.
    while (menuItemEnumerator.MoveNext())
    {

      Message.Text += ((MenuItem)(menuItemEnumerator.Current)).Text + "<br />";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection GetEnumerator 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>
      
      <hr/>

      <asp:label id="Message" 
        runat="server"/>

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


Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.