Share via


Menu.StaticSelectedStyle 属性

定义

获取对 MenuItemStyle 对象的引用,使用该对象可以设置用户在静态菜单中选择的菜单项的外观。

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

属性值

MenuItemStyle 的引用,该对象表示静态菜单中的选定菜单项的样式。

属性

示例

下面的代码示例演示如何使用 StaticSelectedStyle 属性为所选静态菜单项指定浅蓝色背景色。


<%@ 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>Menu StaticSelectedStyle Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu StaticSelectedStyle Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        runat="server">
        
        <staticselectedstyle backcolor="LightBlue"
          borderstyle="Solid"
          bordercolor="Black"
          borderwidth="1"/>
      
        <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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Menu StaticSelectedStyle Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>Menu StaticSelectedStyle Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        runat="server">
        
        <staticselectedstyle backcolor="LightBlue"
          borderstyle="Solid"
          bordercolor="Black"
          borderwidth="1"/>
      
        <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>

注解

StaticSelectedStyle使用 属性可以控制用户在静态菜单中选择的菜单项的外观。 此属性为只读;但是,可以设置它返回的对象 MenuItemStyle 的属性。 属性可以声明性地以 形式 Property-Subproperty进行设置,其中 Subproperty 是对象的属性 MenuItemStyle (例如 StaticSelectedStyle-ForeColor ,) 。 还可以以编程方式以格式 Property.Subproperty (设置属性, StaticSelectedStyle.ForeColor 例如,) 。

静态菜单项的样式属性按以下顺序应用:

  1. StaticMenuStyle.

  2. StaticMenuItemStyle. LevelMenuItemStyles如果定义了集合或LevelSubMenuStyles集合,则此时会应用该集合,并替代其他菜单项样式属性。

  3. StaticSelectedStyle. LevelSelectedStyles如果定义了集合,则此时将应用该集合,替代其他菜单项样式属性。

  4. StaticHoverStyle.

重要

以下中断性变更。

在 .NET Framework 4.0 之前,在 .aspx 文件中定义控件<asp:Menu>时,用户可以设置 StaticSelectedStyle-CssClass 属性以设置所选控件项的 CSS 类。 例如,用户可能有一个菜单控件,如下所示:

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal"   
    StaticMenuItemStyle-CssClass="MenuItem"  
    StaticSelectedStyle-CssClass="MenuItem_selected"  
    OnMenuItemClick="Menu1_MenuItemClick" Style="margin-bottom: 0px" EnableTheming="True" ClientIDMode="Static">  
    <Items>  
        <asp:MenuItem Text="item_0" Value="0" Selected="True"></asp:MenuItem>  
        <asp:MenuItem Text="itme_1" Value="1"></asp:MenuItem>  
    </Items>  
</asp:Menu>  

菜单控件呈现为:

<div id="Menu1" style="margin-bottom: 0px">  
    <ul class="level1">  
        <li><a class="level1 MenuItem MenuItem_selected " href="#" onclick="__doPostBack(&#39;Menu1&#39;,&#39;0&#39;)">item_0</a></li>  
        <li><a class="level1 MenuItem" href="#" onclick="__doPostBack(&#39;Menu1&#39;,&#39;1&#39;)">itme_1</a></li>  
    </ul>  
</div>  

但是,在 .NET Framework 4.0 及更高版本中,所选菜单项始终设置为 selected CSS 类,而不是 指定的StaticSelectedStyle-CssClass类。 因此,上述 ASP.NET 代码呈现为:

<div id="Menu1" style="margin-bottom: 0px">  
    <ul class="level1">  
        <li><a class="level1 MenuItem selected " href="#" onclick="__doPostBack(&#39;Menu1&#39;,&#39;0&#39;)">item_0</a></li>  
        <li><a class="level1 MenuItem" href="#" onclick="__doPostBack(&#39;Menu1&#39;,&#39;1&#39;)">itme_1</a></li>  
    </ul>  
</div>  

适用于

另请参阅