Attention: We are retiring the ASP.NET Community Blogs. Learn more >

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

7 Comments

  • 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

  • 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.

  • Welcome friends! ,

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

  • 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.

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

  • Yes, I have become that grad student. ,

Comments have been disabled for this content.