How to access controls inside a nested homepage? why does it behave differently than content pages?

Is there a difference between these two scenarios:

(1) Access to the resource on the main page from a regular child

(2) Access to the property on the main page from the attached main page

I tried to access the text block on the main page from this page:

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 the nested main 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 

it doesnโ€™t work, am I missing something? I call both codes inside the regulare page_load handler ...

I also noticed that I canโ€™t set the value of the text box inside the nested main page from the code behind, definitely something is 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 AM NOTHING, why did I miss? note that I also tried recursive search:

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

still nothing

+7
nested master-pages
source share
4 answers

I read a little 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, which you can catch to set any fields, so the answer was: on the subpage, do the following:

 protected override void OnLoad(EventArgs e) { myTextBox.Text = "anything"; base.OnLoad(e); } 
+4
source share
 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 
+5
source share

This should work without any problems, so something else should be wrong. I just tried this as part of a simple test project, and I have no problem finding the control on the main page in both cases.

I would check (again) if you are linking to the correct master page inside your nested master page. What you can also check out is the type of Master runtime inside the embedded main page. This should be the type of your homepage.

EDIT . I thought the problem was finding the control on the root main page from the nested main page, and this should work without problems. To find the control inside the content placeholder on the nested homepage, see the following forum post .

+2
source share

You can have absolute control over your content both on the main page and on a subpage on your content page using directives:

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

See C. Scott Allen's excellent article on Ode To Code

0
source share

All Articles