IndexOf Method

MenuItemCollection.IndexOf Method (MenuItem)

 

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

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

public int IndexOf(
	MenuItem value
)

Parameters

value
Type: System.Web.UI.WebControls.MenuItem

The MenuItem to locate.

Return Value

Type: System.Int32

The zero-based index of the first occurrence of value within the current MenuItemCollection, if found; otherwise, -1.

Use the IndexOf method to determine the index of the specified MenuItem object in the current MenuItemCollection object. If the menu item is not in the collection, the method returns -1.

System_CAPS_noteNote

If the specified menu item appears multiple times in the collection, the index of the first occurrence is returned.

The following code example demonstrates how to use the IndexOf method to determine the index of a MenuItem object within a MenuItemCollection object.


<%@ 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 NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
  {

    // Retrieve the parent menu item of the selected menu item.
    MenuItem parent = e.Item.Parent;

    // Determine whether the selected menu item is a 
    // root menu item. Root menu items are stored in
    // a Menu control's Items collection. Other menu
    // items are stored in the parent menu item's
    // ChildItems collection.
    if (parent != null)
    {
      Message.Text = "The " + e.Item.Text + 
        " menu item has an index of " +
        parent.ChildItems.IndexOf(e.Item).ToString() +
        " in the parent menu item's ChildItems collection.";
    }
    else
    {
      Message.Text = "The " + e.Item.Text +
        " menu item has an index of " +
        NavigationMenu.Items.IndexOf(e.Item).ToString() +
        " in the Menu control's Items collection.";
    }

  }

</script>

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

      <h3>MenuItemCollection IndexOf Example</h3>

      Select an item from the menu.
      <br/><br/>

      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank"
        onmenuitemclick="NavigationMenu_MenuItemClick"  
        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>

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft