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>

備註

MenuItemCollection類別是用來儲存和管理 MenuItem 控制項中的物件集合 MenuThe MenuItemCollection class is used to store and manage a collection of MenuItem objects in the Menu control. Menu控制項會使用 MenuItemCollection 類別,將其根功能表項目儲存在屬性中 ItemsThe 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:

  • Item[] 可以使用索引子, MenuItem 在以零為基底的特定索引處直接取出物件。Use the Item[] 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 由新增和移除物件,以程式設計方式管理物件 MenuItemYou can programmatically manage a MenuItemCollection object by adding and removing MenuItem objects. 若要將功能表項目加入至集合,請使用 AddAddAt 方法。To add menu items to the collection, use the Add or the AddAt method. 若要從集合中移除節點,請使用 RemoveRemoveAtClear 方法。To remove nodes from the collection, use the Remove, the RemoveAt, or the Clear method.

注意

當控制項系結 Menu 至資料來源時, Items 和集合會在每次進行系結時 ChildItems 自動填入。When 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.

適用於

另請參閱