从内容页面获取嵌套母版页的元素

时间:2014-06-16 15:29:27

标签: c# asp.net visual-studio-2010 master-pages content-pages

我有一个母版页,一个嵌套的母版页和一个内容页: 主站(的Site.Master):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server"> 
</head>
<body>
    <form runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server" />

     <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false"
                    IncludeStyleBlock="true" Orientation="Horizontal" RenderingMode="List">
                    <StaticSelectedStyle BackColor="LightBlue" BorderStyle="Solid" BorderColor="Black"
                        BorderWidth="1" />
                    <Items>
                        <asp:MenuItem NavigateUrl="~/xxx.aspx" Text="xxx" />
                        <asp:MenuItem NavigateUrl="~/xxx/xxx/xxx.aspx" Text="xxx" />

                    </Items>
                </asp:Menu>
     <div class="main">
        <asp:ContentPlaceHolder ID="cuerpo" runat="server" />
     </div>
    </form>
</body>
</html>

嵌套母版页(mOperator.master)

<%@ Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="mOperator.master.cs"
    Inherits="aplicacion_operadores_mOperador" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="cuerpo" runat="Server">
    <div class="clear hideSkiplink" id="capaMenu">
        <asp:Menu ID="subMenuOperator" runat="server" CssClass="menu" EnableViewState="false"
            IncludeStyleBlock="true" Orientation="Vertical" RenderingMode="List">
            <StaticSelectedStyle BackColor="LightBlue" BorderStyle="Solid" BorderColor="Black"
                BorderWidth="1" />
            <Items>
                <asp:MenuItem NavigateUrl="~/yyyy.aspx" Text="yyy" />
                <asp:MenuItem NavigateUrl="~/yyy/yyy/yyyy.aspx" Text="yyyy" />

            </Items>
        </asp:Menu>
    </div>
    <asp:ContentPlaceHolder ID="masterRight" runat="server">
    </asp:ContentPlaceHolder>
</asp:Content>

内容页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/yyy/yyyy/mOperator.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="operators_Default" %>


<asp:Content ID="Content1" ContentPlaceHolderID="masterRight" Runat="Server">

</asp:Content>

现在,如果我想从内容访问母版页的菜单:

 Menu miPrincipal = (Menu)Master.Master.FindControl("NavigationMenu");
        miPrincipal.Items[1].Selected = true;

我成功地获得了价值。但...

如果我想访问嵌套母版页的菜单。我这样想:

 Menu miSecundario = (Menu)Master.FindControl("subMenuOperator");
 miSecundario.Items[1].Selected = true;

但是给我空了。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

尝试这样的事情

Menu mymenu = this.Page.Master.FindControl("cuerpo").FindControl("Content2").FindControl("subMenuOperator") as Menu;

答案 1 :(得分:0)

我明白了:

 Menu mymenu = this.Page.Master.Master.FindControl("cuerpo").FindControl("subMenuOperator") as Menu;

答案 2 :(得分:0)

您应该使用strongly typed Master Page

在母版页mOperator中显示菜单:

public Menu SubMenuOperator
{
    get
    {
        return this.subMenuOperator;
    }
}

将主类型添加到内容页面

<%@ MasterType VirtualPath="~/mOperator.master" %>

然后您可以访问菜单:

 Menu menu = this.Master.SubMenuOperator;