This documentation is archived and is not being maintained.

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)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class MenuItemStyleCollection : StateManagedCollection

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"




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 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