Export (0) Print
Expand All

MenuItem.ChildItems Property

Note: This property is new in the .NET Framework version 2.0.

Gets a MenuItemCollection object that contains the submenu items of the current menu item.

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

public MenuItemCollection ChildItems { get; }
/** @property */
public MenuItemCollection get_ChildItems ()

public function get ChildItems () : MenuItemCollection

Property Value

A MenuItemCollection that contains the submenu items of the current menu item. The default is a null reference (Nothing in Visual Basic), which indicates that this menu item does not contain any submenu items.

Use the ChildItems property (collection) to access the submenu items of the current menu item, if any. This collection contains only the menu items at the next level. To access menu items further down the menu tree, use the ChildItems property of a subsequent menu item. If the ChildItems property is a null reference (Nothing in Visual Basic), the current menu does not have any submenu items.

The ChildItems property can also be used to programmatically manage the submenu items of the current menu item. You can add, insert, remove, retrieve, and modify MenuItem objects from the collection. Any updates to the collection will automatically be reflected in the Menu control the next time the page is refreshed.

The following code example demonstrates how to use the ChildItems property to traverse the menu items in a Menu control.


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

<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    // If the Menu control contains any root nodes, perform a
    // preorder traversal of the tree and display the text of 
    // each node.
    if (NavigationMenu.Items.Count > 0)
    {

      // Iterate through the root menu items in the Items collection.
      foreach (MenuItem item in NavigationMenu.Items)
      {

        // Display the menu items.
        DisplayChildMenuText(item);

      }

    }
    else
    {

      Message.Text = "The Menu control does not have any items.";

    }
  }

  void DisplayChildMenuText(MenuItem item)
  {

    // Display the menu item's text value.
    Message.Text += item.Text + "<br>";

    // Iterate through the child menu items of the parent menu item 
    // passed into this method, and display their values.
    foreach (MenuItem childItem in item.ChildItems)
    {

      // Recursively call the DisplayChildMenuText method to
      // traverse the tree and display all child menu items.
      DisplayChildMenuText(childItem);

    }

  }
  
</script>

<html>
  <body>
    <form runat="server">
    
      <h3>Menu Items Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        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 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft