MenuItemStyleCollection Class

Represents a collection of MenuItemStyle objects in a Menu control. This class cannot be inherited.

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

public sealed class MenuItemStyleCollection : StateManagedCollection
public final class MenuItemStyleCollection extends StateManagedCollection
public final class MenuItemStyleCollection extends StateManagedCollection
Not applicable.

The MenuItemStyleCollection class is used to store and manage a collection of MenuItemStyle objects in a Menu control. The Menu control uses the MenuItemStyleCollection class as the underlying data type of the LevelMenuItemStyles and LevelSelectedStyles properties.

The LevelMenuItemStyles and LevelSelectedStyles properties are an alternative to the individual style properties (such as StaticMenuItemStyle). These properties are applied to regular menu items and the selected menu item, respectively, based on the menu item's level in the menu. The first style in the collection corresponds to the menu item style for the first level of the menu. The second style in the collection corresponds to the menu item style for the second level of the menu, and so on. The LevelMenuItemStyles and LevelSelectedStyles properties are most often used to generate menus where menu items at a certain level have the same appearance, regardless of whether they have submenus.

The MenuItemStyleCollection class inherits most of its members from the StateManagedCollection class. For more information on the inherited members, see StateManagedCollection.

The following code example demonstrates how to use the MenuItemStyleCollection class to specify the style settings for the menu items in a Menu control based on the menu item's level. In this example, the LevelMenuItemStyles property is created declaratively, and one MenuItemStyle object is removed and another added to the MenuItemStyleCollection object.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void Page_Load(Object sender, EventArgs e)

    if (!IsPostBack)

      // Use the Add and RemoveAt methods to programmatically 
      // remove the third level menu item style and replace 
      // it with a new style, in this case replacing the green background
        // and yellow text with the blue background and white text. 

        MenuItemStyle newStyle = new MenuItemStyle();
        newStyle.BackColor = System.Drawing.Color.Blue;
        newStyle.ForeColor = System.Drawing.Color.White;

        // Remove the last of the three menu item styles. Note that
        // since the collection has a zero-based index, the third
        // entry has an index value of 2.




<html xmlns="" >
  <head runat="server">
    <title>MenuItemStyleCollection Example</title>
    <form id="form1" runat="server">
      <h3>MenuItemStyleCollection Example</h3>
         <!--Add MenuItemStyle objects to the MenuItemStyleCollection -->
         <!--using LevelMenuItemStyles.  -->
         <!--Note that each menu item style represents a level in the menu -->

      <asp:Menu id="MainMenuID"
       Font-Names= "Arial"
         <asp:MenuItemStyle BackColor="Azure" 
             ForeColor="Black" />
           <asp:MenuItemStyle BackColor="Black" 
             ForeColor="White" />
         <asp:MenuItemStyle BackColor="Green" 
             ForeColor="Yellow" />

          <asp:menuitem text="Home"
            <asp:menuitem text="Music"
              <asp:menuitem text="Classical"
              <asp:menuitem text="Rock"
              <asp:menuitem text="Jazz"
            <asp:menuitem text="Movies"
              <asp:menuitem text="Action"
              <asp:menuitem text="Drama"
              <asp:menuitem text="Musical"



  • AspNetHostingPermission  for running the MenuItemStyleCollection control in a hosted environment. Associated enumeration: Minimal


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

Community Additions