0

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?

Za7pi
  • 1,338
  • 7
  • 22
  • 33

3 Answers3

2

Try something like this

Menu mymenu = this.Page.Master.FindControl("cuerpo").FindControl("Content2").FindControl("subMenuOperator") as Menu;
dansasu11
  • 875
  • 1
  • 9
  • 17
  • Null again. If I try to get only the content: `ContentPlaceHolder holder = (ContentPlaceHolder)this.Page.Master.FindControl("Content2");` It is giving me null also. – Za7pi Jun 16 '14 at 16:10
  • 1
    try this and see ContentPlaceHolder holder = (ContentPlaceHolder)this.Page.Master.FindControl("cuerpo").FindControl("Content2"); – dansasu11 Jun 16 '14 at 16:22
0

I got it:

 Menu mymenu = this.Page.Master.Master.FindControl("cuerpo").FindControl("subMenuOperator") as Menu;
Za7pi
  • 1,338
  • 7
  • 22
  • 33
0

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;
NicoD
  • 1,179
  • 8
  • 19