How to refer Master page content in Content Page

Hi,

 

Many a times while using a master page we want to refer content, Controls and properties in Master Page while working in Content page. We come across requirement where by we can change or access the values of controls residing in Master page when we are coding in the content page itself.

 

We can write code in the content pages to access properties, methods and controls in the master page, but there are some limits to it. We can only work with those properties and methods which are declared public. For controls we can refer any control on the master page independent of referencing public member.

 

We can use the @MasterPage directive in the aspx to make the Master property of the content type to be strongly typed. This helps in writing code fro public properties and methods of the master page to be easily coded in compile time.

 

To refer a control I the master page we need to use the FindControl method of the Master property. The controls in the master page can be inside the content place holder in the master page or outside the content place holder of the master page. Both can be easily referred in the content page. For referring to control inside the content place holder in side Master page we first need to refer the content placeholder and then use its FindControl method. Here is how to do it.

 

// for control Inside Content Placeholder

ContentPlaceHolder masterPagePlaceHolder;

TextBox masterPageTextBox;

masterPagePlaceHolder =(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

if(masterPagePlaceHolder != null)
{
       masterPageTextBox =(TextBox) masterPagePlaceHolder.FindControl("TextBox1");
}

 

// for control outside Content Placeholder

Label masterPageLabel = (Label) Master.FindControl("masterPageLabel");

 

Vikram

Published Wednesday, February 27, 2008 3:44 AM by vik20000in

Comments

# re: How to refer Master page content in Content Page

Wednesday, February 27, 2008 3:59 AM by Maciej Kuczara (ABB)

Its pretty bad approach - you rely on find control and string to locate control. Just use properties in master then just cast and you are set ((YourMaster)Page.Master).YourTextBox

# re: How to refer Master page content in Content Page

Wednesday, February 27, 2008 2:41 PM by Joe

To further build upon the properties approach, you can define an interface with the properties in case you have multiple master pages. You then cast the master to the interface and use the property. Or you could create a masterpage base class for your masterpages with either virtual or abstract properties.

# re: How to refer Master page content in Content Page

Thursday, February 28, 2008 8:57 AM by Maciej Kuczara (ABB)

Joe is absolutely correct.Having base class for master pages will greatly help you. You can expand ths approach even more -for handling events raised from master page (like click etc). You can subscribe your controls to master page events.

# re: How to refer Master page content in Content Page

Saturday, July 05, 2008 4:17 AM by Josh

Welcome friends! ,

# re: How to refer Master page content in Content Page

Wednesday, September 03, 2008 10:59 PM by Girish

which page will be rendered first master page or content page??

# re: How to refer Master page content in Content Page

Wednesday, September 17, 2008 9:35 AM by Michael

This is great. I am working on a similar problem...

The above solution can be used if you want to set a control in the content page to a value of a control in the MasterPage.

What if I want to set the property of a control in the MasterPage to the value of a property in a content page? I currently have a Panel in my MasterPage which I need to change the BackImageUrl property on a "content page"-by-page basis.

Any help would be much appreciated.

# re: How to refer Master page content in Content Page

Thursday, October 22, 2009 7:11 AM by His_wife97

Every flash designer worth his salt knows that SWFObject is the only true way to embed flash into a webpage. ,

# re: How to refer Master page content in Content Page

Friday, October 23, 2009 6:21 AM by Settor80

Yes, I have become that grad student. ,

Leave a Comment

(required) 
(required) 
(optional)
(required)