Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 811
Ayyash Avatar asked May 17 '09 10:05

Ayyash


People also ask

How do you access master page controls from the content page?

we create the instance of master page button click event before content page raises its page_load event. To access the master page controls we need to use the Master key word then call the Property method of control. As we use Master. EnterNameTextBox() to access the master page EnterName textbox and Master.

How does a content page differ from a master page in HTML?

A master page provides a template for other pages, with shared layout and functionality. The master page defines placeholders for the content, which can be overridden by content pages. The output result is a combination of the master page and the content page. The content pages contain the content you want to display.

What is the relationship between content page and master page?

master page. In the content page, you create the content by adding Content controls and mapping them to ContentPlaceHolder controls on the master page. For example, the master page might have content placeholders called Main and Footer.

What is master page why it is needed give advantage of it also explain nested master page?

Master pages can be nested, with one master page referencing another as its master. Nested master pages allow you to create componentized master pages. For example, a large site might contain an overall master page that defines the look of the site.


2 Answers

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
like image 134
R0nn1e Avatar answered Sep 23 '22 08:09

R0nn1e


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);
    }
like image 39
Ayyash Avatar answered Sep 22 '22 08:09

Ayyash