MenuItemCollection 類別

定義

表示 Menu 控制項中功能表項目的集合。 此類別無法獲得繼承。

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

範例

下列程式碼範例示範如何使用宣告式語法填入 ItemsChildItems 集合。


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

下列程式碼範例示範如何以程式設計方式將 物件 ChildItems 加入 MenuItem 根功能表項目的集合。


<%@ 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 是用來儲存和管理 控制項中的 Menu 物件集合 MenuItem 。 控制項 MenuMenuItemCollection 使用 類別,將其根功能表項目儲存在 屬性中 Items 。 如果有任何) ,這個集合也會用於 ChildItems 物件的 屬性 MenuItem ,以儲存功能表項目的子功能表項 (。

類別 MenuItemCollection 支援數種方式來存取集合中的專案:

  • Item[]使用索引子直接擷取特定 MenuItem 以零起始之索引處的物件。

  • GetEnumerator使用 方法來建立可用來逐一查看集合的列舉值。

  • CopyTo使用 方法,將集合的內容複寫到陣列中。

您可以藉由新增和移除 MenuItem 物件,以程式設計方式管理 MenuItemCollection 物件。 若要將功能表項目新增至集合,請使用 AddAddAt 方法。 若要從集合中移除節點,請使用 RemoveRemoveAtClear 方法。

注意

Menu當控制項系結至資料來源時,每次進行系結時, Items 都會自動填入 和 ChildItems 集合。 系結之間集合的任何變更都會遺失。 若要保留這些變更,請更新資料來源,或在每次系結時手動重建集合。

類別 MenuItemCollection 包含屬性和方法,可讓您擷取集合本身的相關資訊。 若要瞭解集合中的專案數目,請使用 Count 屬性。 如果您想要判斷集合是否包含特定 MenuItem 物件,請使用 Contains 方法。 若要取得集合中物件的索引 MenuItem ,請使用 IndexOf 方法。

建構函式

MenuItemCollection()

使用預設值,初始化 MenuItemCollection 類別的新執行個體。

MenuItemCollection(MenuItem)

使用指定的父功能表項目 (或擁有人),初始化 MenuItemCollection 類別的新執行個體。

屬性

Count

取得目前 MenuItemCollection 物件中所包含的功能表項目數。

IsSynchronized

取得值,表示是否要同步處理 (執行緒安全) 對 MenuItemCollection 物件的存取。

Item[Int32]

取得目前 MenuItem 物件中位於指定索引處的 MenuItemCollection 物件。

SyncRoot

取得可用來同步處理對 MenuItemCollection 物件之存取的物件。

方法

Add(MenuItem)

將指定的 MenuItem 物件附加到目前 MenuItemCollection 物件的結尾。

AddAt(Int32, MenuItem)

將指定的 MenuItem 物件插入指定索引位置的目前 MenuItemCollection 物件。

Clear()

從目前 MenuItemCollection 物件移除所有項目。

Contains(MenuItem)

判斷指定的 MenuItem 物件是否在集合中。

CopyTo(Array, Int32)

從目標陣列中的指定索引處開始,將 MenuItemCollection 物件中的所有項目複製到相容的一維 Array 中。

CopyTo(MenuItem[], Int32)

從目標陣列中的指定索引開始,從 MenuItemCollection 物件中,將所有項目複製至相容的一維陣列 MenuItem 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回列舉值,可用來逐一查看在目前 MenuItemCollection 物件中的項目。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(MenuItem)

判斷集合中指定之 MenuItem 物件的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(MenuItem)

MenuItem 物件移除指定的 MenuItemCollection 物件。

RemoveAt(Int32)

從目前 MenuItem 物件中移除指定索引位置的 MenuItemCollection 物件。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IStateManager.IsTrackingViewState

取得值,指出 MenuItemCollection 物件是否正在儲存變更至它的檢視狀態。

IStateManager.LoadViewState(Object)

載入 MenuItemCollection 物件先前儲存的檢視狀態。

IStateManager.SaveViewState()

將檢視狀態的變更儲存至 Object

IStateManager.TrackViewState()

指示 MenuItemCollection 物件追蹤其檢視狀態的變更。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱