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