MenuItemCollection 类

定义

表示 Menu 控件中菜单项的集合。Represents a collection of menu items in a Menu control. 此类不能被继承。This class cannot be inherited.

public ref class MenuItemCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class MenuItemCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type MenuItemCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class MenuItemCollection
Implements ICollection, IStateManager
继承
MenuItemCollection
实现

示例

下面的代码示例演示如何使用声明性Items语法ChildItems填充和集合。The following code example demonstrates how to populate the Items and ChildItems collections using declarative syntax.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

  <!-- For the hover styles of the Menu control to  -->
  <!-- work correctly, you must include this head   -->
  <!-- element.                                     -->
  <head runat="server">
    <title>Menu Declarative Example</title>
</head>

  <body>
    <form id="form1" runat="server">
    
      <h3>Menu Declarative Example</h3>
    
      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Submenu items are      -->
      <!-- created by nesting them in parent menu -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"  
        runat="server">
        
        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>
      
        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>
      
      </asp:menu>

    </form>
  </body>
</html>


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

  <!-- For the hover styles of the Menu control to  -->
  <!-- work correctly, you must include this head   -->
  <!-- element.                                     -->
  <head runat="server">
    <title>Menu Declarative Example</title>
</head>

  <body>
    <form id="form1" runat="server">
    
      <h3>Menu Declarative Example</h3>
    
      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Submenu items are      -->
      <!-- created by nesting them in parent menu -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"  
        runat="server">
        
        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>
      
        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>
      
      </asp:menu>

    </form>
  </body>
</html>

下面的代码示例演示如何以编程方式将MenuItem对象添加ChildItems到根菜单项的集合。The following code example demonstrates how to programmatically add a MenuItem object to the ChildItems collection of a root menu item.


<%@ 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 (!IsPostBack)
    {
      // Retrieve the root menu item from the Items
      // collection of the Menu control using the indexer.
      MenuItem homeMenuItem = NavigationMenu.Items[0];

      // Create the submenu item.
      MenuItem newSubMenuItem = new MenuItem("New Category");

      // Add the submenu item to the ChildItems
      // collection of the root menu item.
      homeMenuItem.ChildItems.Add(newSubMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection Add Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection Add Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        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>

    </form>
  </body>
</html>


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then

      ' Retrieve the root menu item from the Items
      ' collection of the Menu control using the indexer.
      Dim homeMenuItem As MenuItem = NavigationMenu.Items(0)

      ' Create the submenu item.
      Dim newSubMenuItem = New MenuItem("New Category")

      ' Add the submenu item to the ChildItems
      ' collection of the root menu item.
      homeMenuItem.ChildItems.Add(newSubMenuItem)
    
    End If
      
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection Add Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection Add Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        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>

    </form>
  </body>
</html>

注解

类用于存储和管理Menu控件中对象的MenuItem集合。 MenuItemCollectionThe MenuItemCollection class is used to store and manage a collection of MenuItem objects in the Menu control. 控件使用类将其根菜单项存储在Items属性中。 MenuItemCollection MenuThe Menu control uses the MenuItemCollection class to store its root menu items in the Items property. 此集合还用于ChildItems MenuItem对象的属性, 以存储菜单项的子菜单项 (如果有)。This collection is also used for the ChildItems property of a MenuItem object to store a menu item's submenu items (if any).

MenuItemCollection类支持多种方法来访问集合中的项:The MenuItemCollection class supports several ways to access the items in the collection:

  • 使用索引器可以直接检索特定MenuItem的从零开始的索引处的对象。 Item[Int32]Use the Item[Int32] indexer to directly retrieve a MenuItem object at a specific zero-based index.

  • GetEnumerator使用方法可创建一个可用于循环访问集合的枚举器。Use the GetEnumerator method to create an enumerator that can be used to iterate through the collection.

  • CopyTo使用方法可将集合的内容复制到数组中。Use the CopyTo method to copy the contents of the collection into an array.

可以通过添加和删除MenuItemCollection MenuItem对象以编程方式管理对象。You can programmatically manage a MenuItemCollection object by adding and removing MenuItem objects. 若要向集合中添加菜单项, 请Add使用AddAt或方法。To add menu items to the collection, use the Add or the AddAt method. 若要从集合中删除节点, 请Remove使用RemoveAt、或Clear方法。To remove nodes from the collection, use the Remove, the RemoveAt, or the Clear method.

备注

将控件绑定到数据源后, 每次发生Items绑定ChildItems时, 将自动填充和集合。 MenuWhen the Menu control is bound to a data source, the Items and ChildItems collections are automatically populated each time binding occurs. 绑定之间对集合所做的任何更改都将丢失。Any changes to the collections between bindings will be lost. 若要保留这些更改, 请更新数据源, 或者在每次绑定时手动重新生成集合。To retain these changes, either update the data source or manually rebuild the collection each time you bind.

MenuItemCollection类包含的属性和方法可用于检索有关集合本身的信息。The MenuItemCollection class contains properties and methods that allow you to retrieve information about the collection itself. 若要确定集合中有多少项, 请使用Count属性。To find out how many items are in the collection, use the Count property. 如果要确定集合是否包含MenuItem某个对象, 请Contains使用方法。If you want to determine whether the collection contains a certain MenuItem object, use the Contains method. 若要获取集合中MenuItem对象的索引, 请IndexOf使用方法。To get the index of a MenuItem object in the collection, use the IndexOf method.

构造函数

MenuItemCollection()

用默认值初始化 MenuItemCollection 类的新实例。Initializes a new instance of the MenuItemCollection class using the default values.

MenuItemCollection(MenuItem)

使用指定的父菜单项(或所有者)初始化 MenuItemCollection 类的新实例。Initializes a new instance of the MenuItemCollection class using the specified parent menu item (or owner).

属性

Count

获取当前 MenuItemCollection 对象所含菜单项的数目。Gets the number of menu items contained in the current MenuItemCollection object.

IsSynchronized

获取一个值,该值指示对 MenuItemCollection 对象的访问是否同步(线程安全)。Gets a value indicating whether access to the MenuItemCollection object is synchronized (thread safe).

Item[Int32]

获取当前 MenuItem 对象中指定索引处的 MenuItemCollection 对象。Gets the MenuItem object at the specified index in the current MenuItemCollection object.

SyncRoot

获取一个对象,该对象可用于同步对 MenuItemCollection 对象的访问。Gets an object that can be used to synchronize access to the MenuItemCollection object.

方法

Add(MenuItem)

将指定的 MenuItem 对象追加到当前 MenuItemCollection 对象的末尾。Appends the specified MenuItem object to the end of the current MenuItemCollection object.

AddAt(Int32, MenuItem)

将指定的 MenuItem 对象插入到当前 MenuItemCollection 对象的指定索引位置。Inserts the specified MenuItem object in the current MenuItemCollection object at the specified index location.

Clear()

从当前 MenuItemCollection 对象中移除所有项。Removes all items from the current MenuItemCollection object.

Contains(MenuItem)

确定指定的 MenuItem 对象是否在集合中。Determines whether the specified MenuItem object is in the collection.

CopyTo(Array, Int32)

从目标数组的指定索引开始,将 MenuItemCollection 对象中的所有项复制到兼容的一维 Array 中。Copies all the items from the MenuItemCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

CopyTo(MenuItem[], Int32)

从目标数组的指定索引开始,将 MenuItemCollection 对象中的所有项复制到 MenuItem 对象的一维兼容数组。Copies all the items from the MenuItemCollection object to a compatible one-dimensional array of MenuItem objects, starting at the specified index in the target array.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回一个枚举数,该枚举数可用于循环访问当前 MenuItemCollection 对象中的项。Returns an enumerator that can be used to iterate through the items in the current MenuItemCollection object.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IndexOf(MenuItem)

确定集合中指定 MenuItem 对象的索引。Determines the index of the specified MenuItem object in the collection.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Remove(MenuItem)

MenuItem 对象中移除指定的 MenuItemCollection 对象。Removes the specified MenuItem object from the MenuItemCollection object.

RemoveAt(Int32)

从当前 MenuItem 对象中移除指定索引位置的 MenuItemCollection 对象。Removes the MenuItem object at the specified index location from the current MenuItemCollection object.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

IStateManager.IsTrackingViewState

获取一个值,该值指示 MenuItemCollection 对象是否正将更改保存到其视图状态。Gets a value that indicates whether the MenuItemCollection object is saving changes to its view state.

IStateManager.LoadViewState(Object)

加载 MenuItemCollection 对象先前保存的视图状态。Loads the MenuItemCollection object's previously saved view state.

IStateManager.SaveViewState()

将视图状态的更改保存到 ObjectSaves the changes to view state to an Object.

IStateManager.TrackViewState()

指示 MenuItemCollection 对象跟踪对其视图状态的更改。Instructs the MenuItemCollection object to track changes to its view state.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅