Menu.Items 屬性

定義

取得 MenuItemCollection 物件,這個物件含有 Menu 控制項中所有的功能表項目。

public:
 property System::Web::UI::WebControls::MenuItemCollection ^ Items { System::Web::UI::WebControls::MenuItemCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.MenuItemCollection Items { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.Items : System.Web.UI.WebControls.MenuItemCollection
Public ReadOnly Property Items As MenuItemCollection

屬性值

MenuItemCollection,包含 Menu 控制項中所有的功能表項目。

屬性

範例

下列程式碼範例示範如何使用 Items 集合來周遊 控制項中的 Menu 功能表項目。


<%@ 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 the Menu control contains any root nodes, perform a
    // preorder traversal of the tree and display the text of 
    // each node.
    if (NavigationMenu.Items.Count > 0)
    {

      // Iterate through the root menu items in the Items collection.
      foreach (MenuItem item in NavigationMenu.Items)
      {

        // Display the menu items.
        DisplayChildMenuText(item);

      }

    }
    else
    {

      Message.Text = "The Menu control does not have any items.";

    }
  }

  void DisplayChildMenuText(MenuItem item)
  {

    // Display the menu item's text value.
    Message.Text += item.Text + "<br />";

    // Iterate through the child menu items of the parent menu item 
    // passed into this method, and display their values.
    foreach (MenuItem childItem in item.ChildItems)
    {

      // Recursively call the DisplayChildMenuText method to
      // traverse the tree and display all child menu items.
      DisplayChildMenuText(childItem);

    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Menu Items Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu Items Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        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>
      
      <hr/>
      
      <asp:label id="Message" 
        runat="server"/>

    </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 the Menu control contains any root nodes, perform a
    ' preorder traversal of the tree and display the text of 
    ' each node.
    If NavigationMenu.Items.Count > 0 Then
    

      ' Iterate through the root menu items in the Items collection.
      Dim item As MenuItem
      For Each item In NavigationMenu.Items

        ' Display the menu items.
        DisplayChildMenuText(item)

      Next

    Else

      Message.Text = "The Menu control does not have any items."

    End If
      
  End Sub

  Sub DisplayChildMenuText(ByVal item As MenuItem)

    ' Display the menu item's text value.
    Message.Text &= item.Text & "<br />"

    ' Iterate through the child menu items of the parent menu item 
    ' passed into this method, and display their values.
    Dim childItem As MenuItem
    For Each childItem In item.ChildItems

      ' Recursively call the DisplayChildMenuText method to
      ' traverse the tree and display all child menu items.
      DisplayChildMenuText(childItem)

    Next

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Menu Items Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu Items Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        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>
      
      <hr/>
      
      <asp:label id="Message" 
        runat="server"/>

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

備註

Items使用 屬性 (集合) 來取得 MenuItemCollection 物件,其中包含 控制項中的所有 Menu 功能表項目。 此集合通常用來快速逐一查看所有功能表項目,或存取特定功能表項目。

集合 Items 也可以用來以程式設計方式管理功能表項目。 您可以新增、插入、移除和擷取 MenuItem 集合物件。 下一次往返伺服器之後, Menu 集合的任何更新都會自動反映在 控制項中。

適用於

另請參閱