How to access controls inside a nested master page? why does it behave differently from content pages?
Asked Answered
R

4

7

Is there a difference between these two scenarios:

(1) Accessing a property on a master page from a regular child

(2) Accessing a property on a master page from a nested master page

I tried to access a textbox in the master page from a content page like this:

TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx

it works, but then when I applied the same method on a nested master page:

TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master

this does not work, am I missing something? I call both codes inside regulare page_load handler...

I also noticed I cannot set textbox value inside the nested master page from code behind, there is definitely something im missing, what is it? To shed light on this issue, here is an example:

Nested Master Page:

<%@ Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>

Code behind:

Response.Wrote(tx2.Text);

I get NOTHING, why what did I miss? note that I also tried the recursive find control:

String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;

still nothing

Riorsson answered 17/5, 2009 at 10:55 Comment(0)
R
4

I read few things here: http://www.odetocode.com/Articles/450.aspx and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:

protected override void OnLoad(EventArgs e)
    {
        myTextBox.Text = "anything";
        base.OnLoad(e);
    }
Riorsson answered 18/5, 2009 at 5:32 Comment(1)
If the nested master page does not call Page_Load, something else is wrong as well because all Page_Load methods should be called (on the master page, the nested master page and the content page). I added some more info to my original answer.Spicate
F
5
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
  //base content place holder id

Label objLabel3 = (Label)cp.FindControl("lblNested");
  //lblNested is id in nested master page
Farfamed answered 15/6, 2012 at 6:16 Comment(0)
R
4

I read few things here: http://www.odetocode.com/Articles/450.aspx and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:

protected override void OnLoad(EventArgs e)
    {
        myTextBox.Text = "anything";
        base.OnLoad(e);
    }
Riorsson answered 18/5, 2009 at 5:32 Comment(1)
If the nested master page does not call Page_Load, something else is wrong as well because all Page_Load methods should be called (on the master page, the nested master page and the content page). I added some more info to my original answer.Spicate
S
2

This should work without any problems, so something else must be wrong. I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases.

I would check (again) if you refer to the correct master page inside your nested master page. What you could also check is the runtime type of the Master property inside your nested master page. It should be the type of your master page.

EDIT: I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. For finding a control inside a content placeholder in a nested master page, take a look at the following forum post.

Spicate answered 17/5, 2009 at 11:59 Comment(2)
i just created a new project from scratch, i still get the same results! i can't access anything on the middle nested pageRiorsson
I thought your question was about accessing properties on the root master page from both a nested master page and a content page. I´ll try and see if I can get the otrher snenario to work: accessing a property on a nested master page from a content page.Spicate
F
0

You can have absolute control of your content in both master page and nested page from your content page using the directives:

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

See the excellent article from K. Scott Allen in Ode To Code

Falmouth answered 6/5, 2015 at 4:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.