简体   繁体   中英

Get element of nested master page from content page

I have got a master page, a nested master page and a content page: Master(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>

Nested master page(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>

Content page:

<%@ 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>

Now, if I want to access the menu of the master page from content:

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

I get the value succesfully. but...

If I want to access the menu of nested master page. I am trying like this:

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

But is giving me null.

Any ideas?

试试这个

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

我知道了:

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

You should use strongly typed Master Page

In your master page mOperator you expose the menu :

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

Add the master type to the content page

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

and then you can access to the menu with :

 Menu menu = this.Master.SubMenuOperator;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM