共用方式為


MenuItem.Selected 屬性

定義

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

public:
 property bool Selected { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(true)]
public bool Selected { get; set; }
[<System.ComponentModel.Browsable(true)>]
member this.Selected : bool with get, set
Public Property Selected As Boolean

屬性值

Boolean

true 表示目前的功能表項目已在 Menu 控制項中選取,否則為 false。 預設為 false

屬性

範例

下列範例示範如何在第一次載入頁面時,使用 Selected 屬性來選取 控制項中的 Menu 特定功能表項目。 若要讓此範例正常運作,您必須將下面的範例網站地圖資料複製到名為 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">
<script runat="server">

  void NavigationMenu_MenuItemDataBound(Object sender, MenuEventArgs e)
  {
    // Get the menu item being bound to data.
    MenuItem item = e.Item;

    // Use the Selected property to select the Home 
    // menu item when the page is first loaded.
    if (item.Text == "Home")
    {
      item.Selected = true;
    }
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Selected Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Selected Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        onmenuitemdatabound="NavigationMenu_MenuItemDataBound" 
        runat="server">
        
        <staticselectedstyle backcolor="Yellow"/>
        
      </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">
<script runat="server">

  Sub NavigationMenu_MenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs)

    ' Get the menu item being bound to data.
    Dim item As MenuItem = e.Item

    ' Use the Selected property to select the Home 
    ' menu item when the page is first loaded.
    If item.Text = "Home" Then
    
      item.Selected = True
    
    End If
      
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Selected Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Selected Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        onmenuitemdatabound="NavigationMenu_MenuItemDataBound" 
        runat="server">
        
        <staticselectedstyle backcolor="Yellow"/>
        
      </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>

備註

Selected使用 屬性來指定或判斷是否已選取目前的功能表項目。

注意

雖然屬性可用來判斷是否已選取功能表項目,但使用 屬性比較常見 SelectedItem

控制項中一次 Menu 只能選取一個功能表項目。

適用於

另請參閱