MenuItem 類別

定義

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

public ref class MenuItem sealed : ICloneable, System::Web::UI::IStateManager
public sealed class MenuItem : ICloneable, System.Web.UI.IStateManager
type MenuItem = class
    interface IStateManager
    interface ICloneable
Public NotInheritable Class MenuItem
Implements ICloneable, IStateManager
繼承
MenuItem
實作

範例

下列範例示範如何使用宣告式語法,以靜態 MenuItem 物件填入 Menu 控制項。


<%@ 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" >
  <head runat="server">
    <title>MenuItem Declarative Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Declarative Example</h3>
    
      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Create submenu items   -->
      <!-- by nesting them within parent menu     -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="1"
        staticsubmenuindent="10" 
        orientation="Vertical" 
        target="_blank"  
        runat="server">

        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            imageurl="Images\Home.gif"
            popoutimageurl="Images\Popout.jpg"   
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              popoutimageurl="Images\Popout.jpg"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              popoutimageurl="Images\Popout.jpg"              
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                separatorimageurl="Images\Separator.jpg"
                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" >
  <head runat="server">
    <title>MenuItem Declarative Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Declarative Example</h3>
    
      <!-- Use declarative syntax to create the   -->
      <!-- menu structure. Create submenu items   -->
      <!-- by nesting them within parent menu     -->
      <!-- items.                                 -->
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="1"
        staticsubmenuindent="10" 
        orientation="Vertical" 
        target="_blank"  
        runat="server">

        <items>
          <asp:menuitem navigateurl="Home.aspx" 
            text="Home"
            imageurl="Images\Home.gif"
            popoutimageurl="Images\Popout.jpg"   
            tooltip="Home">
            <asp:menuitem navigateurl="Music.aspx"
              text="Music"
              popoutimageurl="Images\Popout.jpg"
              tooltip="Music">
              <asp:menuitem navigateurl="Classical.aspx" 
                text="Classical"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Classical"/>
              <asp:menuitem navigateurl="Rock.aspx"
                text="Rock"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Rock"/>
              <asp:menuitem navigateurl="Jazz.aspx"
                text="Jazz"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Jazz"/>
            </asp:menuitem>
            <asp:menuitem navigateurl="Movies.aspx"
              text="Movies"
              popoutimageurl="Images\Popout.jpg"              
              tooltip="Movies">
              <asp:menuitem navigateurl="Action.aspx"
                text="Action"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Action"/>
              <asp:menuitem navigateurl="Drama.aspx"
                text="Drama"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Drama"/>
              <asp:menuitem navigateurl="Musical.aspx"
                text="Musical"
                separatorimageurl="Images\Separator.jpg"
                tooltip="Musical"/>
            </asp:menuitem>
          </asp:menuitem>
        </items>
      
      </asp:menu>

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

下列範例示範如何將控制項系結 MenuSiteMapDataSource 控制項。 系結至資料來源時, Menu 控制項會自動建立 MenuItem 物件。 若要讓此範例正常運作,您必須將下面的範例網站地圖資料複製到名為 Web.sitemap 的檔案。


<%@ 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 DataBinding Example</title>
</head>

  <body>
    <form id="form1" runat="server">
    
      <h3>Menu DataBinding Example</h3>
    
      <!-- Bind the Menu control to a SiteMapDataSource control.  -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"
        datasourceid="MenuSource"   
        runat="server">
        
        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>

      </asp:menu>
      
      <asp:SiteMapDataSource id="MenuSource"
        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">
<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 DataBinding Example</title>
</head>

  <body>
    <form id="form1" runat="server">
    
      <h3>Menu DataBinding Example</h3>
    
      <!-- Bind the Menu control to a SiteMapDataSource control.  -->
      <asp:menu id="NavigationMenu"
        disappearafter="2000"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        font-names="Arial" 
        target="_blank"
        datasourceid="MenuSource"   
        runat="server">
        
        <staticmenuitemstyle backcolor="LightSteelBlue"
          forecolor="Black"/>
        <statichoverstyle backcolor="LightSkyBlue"/>
        <dynamicmenuitemstyle backcolor="Black"
          forecolor="Silver"/>
        <dynamichoverstyle backcolor="LightSkyBlue"
          forecolor="Black"/>

      </asp:menu>
      
      <asp:SiteMapDataSource id="MenuSource"
        runat="server"/>        

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

以下是先前範例的範例網站地圖資料。

<siteMap>

<siteMapNode url="~\Home.aspx"

title="Home"

description="Home">

<siteMapNode url="~\Music.aspx"

title="Music"

description="Music">

<siteMapNode url="~\Classical.aspx"

title="Classical"

description="Classical"/>

<siteMapNode url="~\Rock.aspx"

title="Rock"

description="Rock"/>

<siteMapNode url="~\Jazz.aspx"

title="Jazz"

description="Jazz"/>

</siteMapNode>

<siteMapNode url="~\Movies.aspx"

title="Movies"

description="Movies">

<siteMapNode url="~\Action.aspx"

title="Action"

description="Action"/>

<siteMapNode url="~\Drama.aspx"

title="Drama"

description="Drama"/>

<siteMapNode url="~\Musical.aspx"

title="Musical"

description="Musical"/>

</siteMapNode>

</siteMapNode>

</siteMap>

備註

Menu控制項是由 物件所代表的功能表項目階層所 MenuItem 組成。 每個功能表項目都有唯讀 Depth 屬性,指定控制項中 Menu 顯示功能表項目的層級。 最上層 (層級 0) 沒有父功能表項目的功能表項目稱為根功能表項目。 具有父功能表項目的功能表項目稱為子功能表專案。 所有根功能表項目都會儲存在集合中 Items 。 子功能表專案會儲存在父功能表項目的 ChildItems 集合中。 您可以使用 屬性來存取功能表項目的父功能表項目 Parent

注意

ItemsChildItems 集合只包含下一個層級的功能表項目。 若要進一步存取功能表樹狀目錄下的功能表項目,請使用 ChildItems 後續功能表項目的 屬性。

若要建立控制項的 Menu 功能表項目,請使用下列其中一種方法:

使用宣告式語法來建立靜態功能表項目。

使用建構函式動態建立 類別的新實例 MenuItemMenuItem然後,這些物件可以新增至 ItemsChildItems 集合。

Menu 控制項系結至資料來源。 Menu當控制項系結至 SiteMapDataSource 控制項時, MenuItem 會自動建立對應至資料來源中專案的物件。 對於其他資料來源, MenuItem 也會自動建立符合資料來源階層結構的物件;不過,您也必須使用 DataBindings 集合來定義功能表項目系結,以指定功能表項目與其對應資料項目之間的系結關聯性。

功能表項目會根據其層級,顯示在靜態功能表或動態功能表中。 靜態功能表一律會顯示在 控制項中 Menu 。 根據預設,最上層 (層級 0) 的功能表項目會顯示在靜態功能表中。 您可以藉由設定 StaticDisplayLevels 屬性,在靜態功能表內顯示其他功能表 (靜態子功能表) 。 如果具有高於 屬性所指定 StaticDisplayLevels 值的任何) ,則會在動態子功能表中顯示功能表項目 (。 只有在使用者將滑鼠指標放在包含動態子功能表的父功能表項目上方時,才會顯示動態子功能表。

當使用者按一下功能表項目時, Menu 控制項可以流覽至連結的網頁,或直接回傳至伺服器。 NavigateUrl如果設定功能表項目的 屬性, Menu 控制項會巡覽至連結的頁面,否則會將頁面張貼回伺服器進行處理。 根據預設,連結的頁面會顯示在與控制項相同的視窗或框架 Menu 中。 若要在不同的視窗或框架中顯示連結的內容,請使用 Target 控制項的 Menu 屬性。

注意

屬性 Menu.Target 會影響 控制項中的每個功能表項目。 若要指定個別功能表項目的視窗或框架,請直接設定 Target 物件的 屬性 MenuItem

每個功能表項目都有 TextValue 屬性。 屬性的值 Text 會顯示在 控制項中 Menu ,而 Value 屬性可用來儲存功能表項目的任何其他資料,例如傳遞至與功能表項目相關聯之回傳事件的資料。 如果您設定 Text 屬性,但讓屬性保持 Value 未設定, Value 則屬性會自動設定為 Text 與 屬性相同的值。 反之亦然。 如果您設定 Value 屬性,但不是 Text 屬性,則 Text 屬性會自動設定為與 屬性相同的值 Value

注意

相同功能表層級的功能表項目必須各自具有屬性的唯一值 Value ; Menu 控制項無法區分相同層級具有相同值的不同功能表項目。 在此案例中,如果使用者按一下具有重複值的功能表項目,則會選取功能表中第一個出現的功能表項目。

若要在使用者將滑鼠指標放在功能表項目上方時顯示工具提示,請設定專案的 ToolTip 屬性。

Menu控制項有數種不同類型的功能表項目。 您可以使用下表中的屬性來控制樣式 (,例如不同功能表項目類型的字型大小和色彩) 。

功能表項目樣式屬性 描述
DynamicHoverStyle 當滑鼠指標置於動態功能表項目上方時,動態功能表項目的樣式設定。
DynamicMenuItemStyle 個別動態功能表項目的樣式設定。
DynamicMenuStyle 動態功能表的樣式設定。
DynamicSelectedStyle 目前選取之動態功能表項目的樣式設定。
StaticHoverStyle 當滑鼠指標放在靜態功能表項目上方時,其樣式設定。
StaticMenuItemStyle 個別靜態功能表項目的樣式設定。
StaticMenuStyle 靜態功能表的樣式設定。
StaticSelectedStyle 目前選取之靜態功能表項目的樣式設定。

您可以使用下表所示的樣式集合,來指定套用至功能表項目的樣式,而不是設定個別樣式屬性。

層級樣式集合 描述
LevelMenuItemStyles 物件的集合 MenuItemStyle ,這些物件會根據其層級控制功能表項目的樣式。
LevelSelectedStyles 物件的集合 MenuItemStyle ,可根據其層級控制所選功能表項目的樣式。
LevelSubMenuStyles 物件的集合 MenuItemStyle ,這些物件會根據其層級控制子功能表專案的樣式。

集合中的第一個樣式會對應至功能表樹中第一個深度層級的功能表項目樣式。 集合中的第二個樣式會對應至功能表樹狀結構中第二個深度層級的功能表項目樣式,依此類故。 這最常用來產生目錄樣式導覽功能表,其中特定深度的功能表項目應該具有相同的外觀,不論它們是否有子功能表。

注意

如果您使用上表中所列的任何層級樣式集合來定義控制項的 Menu 樣式,這些樣式設定會覆寫個別功能表項目樣式屬性。

除了自訂功能表項目的樣式之外,您也可以自訂其外觀。 您可以藉由設定下表中的屬性,為功能表項目的不同部分指定自訂影像。

Image 屬性 描述
ImageUrl 功能表項目文字旁顯示的選擇性影像。
PopOutImageUrl 功能表項目中顯示的選擇性影像,表示功能表項目具有動態子功能表。
SeparatorImageUrl 功能表項目底部顯示的選擇性影像,以與其他功能表項目分開。

若要判斷控制項中 Menu 目前是否選取功能表項目,請使用 Selected 屬性。 您也可以使用 DataBound 屬性來判斷功能表項目是否系結至資料。 如果功能表項目系結至資料,您可以使用 DataItem 屬性來存取系結至功能表項目的資料項目值。

如需 MenuItem 類別之執行個體的初始屬性值清單,請參閱 MenuItem 建構函式。

建構函式

MenuItem()

不以功能表文字或值,初始化 MenuItem 類別的新執行個體。

MenuItem(String)

使用指定的功能表文字,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String)

使用指定的功能表文字和值,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String, String)

使用指定的功能表文字、值和影像的 URL,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String, String, String)

使用指定的功能表文字、值、影像 URL 和巡覽 URL,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String, String, String, String)

使用指定的功能表文字、值、影像 URL、巡覽 URL 和目標,初始化 MenuItem 類別的新執行個體。

屬性

ChildItems

取得 MenuItemCollection 物件,其中含有目前功能表項目的子功能表項目。

DataBound

取得值,指出功能表項目是否透過資料繫結所建立。

DataItem

取得繫結至功能表項目的資料項目。

DataPath

取得繫結至功能表項目之資料的路徑。

Depth

取得顯示功能表項目的層級。

Enabled

取得或設定值,指出 MenuItem 物件是否已啟用,讓項目能夠顯示快顯影像及任何子功能表項目。

ImageUrl

取得或設定顯示在功能表項目文字旁的影像之 URL。

NavigateUrl

取得或設定在按一下功能表項目時所要巡覽的 URL。

Parent

取得目前功能表項目的父功能表項目。

PopOutImageUrl

取得或設定顯示在功能表項目中之影像的 URL,表示該功能表項目具有動態子功能表。

Selectable

取得或設定值,指出 MenuItem 物件是否可選取,或是否為「可點選」。

Selected

取得或設定值,指出目前的功能表項目是否已在 Menu 控制項中選取。

SeparatorImageUrl

取得或設定顯示在功能表項目底部,以便與其他功能表項目隔開之影像的 URL。

Target

取得或設定要顯示與功能表項目關聯之 Web 網頁內容的目標視窗或框架。

Text

取得或設定針對 Menu 控制項中的功能表項目所顯示之文字。

ToolTip

取得或設定功能表項目的工具提示文字。

Value

取得或設定非顯示值,這個值是用來儲存與功能表項目相關的其他資料,例如用來處理回傳事件的資料。

ValuePath

取得從根功能表項目通往目前功能表項目的路徑。

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

ICloneable.Clone()

建立目前 MenuItem 物件的複本。

IStateManager.IsTrackingViewState

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

IStateManager.LoadViewState(Object)

載入功能表項目先前儲存的檢視狀態。

IStateManager.SaveViewState()

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

IStateManager.TrackViewState()

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

適用於

另請參閱