MenuItem.ChildItems Property

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)

[BrowsableAttribute(false)]
[PersistenceModeAttribute(PersistenceMode.InnerDefaultProperty)]
public MenuItemCollection ChildItems { get; }

Property Value

Type: System.Web.UI.WebControls.MenuItemCollection
A MenuItemCollection that contains the submenu items of the current menu item. The default is null, 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 null, 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 example demonstrates how to use the ChildItems property to traverse the menu items 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)
  {
    // 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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Menu Items Example</title>
</head>
<body>
    <form id="form1" 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft