Step 2: Set the Master Page property in the page_load() of content page by casting it to MasterPage object.
For Ex:
protected void Page_Load(object sender, EventArgs e)
{
DemoMaster master =(DemoMaster)Master ;
master.HeadingText = “Content Page #1”;
}
Where “DemoMaster” is an instance of Master Page, and HeadingText is the Public Property of that MasterPage class.
Sample Demonstration:
1. Create a sample web application named WebDemoMaster which consists of Master Page (DemoMaster.aspx) and ContentPages (ContentPage1.aspx, ContentPage 2. aspx).
2. The master page consists of a label (lblTitleContent ) displaying “MasterPage”(default value), ContentPlaceHolder for displaying Content Pages, and a navigational pane for navigating to different content pages.
3. Define a public property named HeadingText in the DemoMaster.aspx.cs. The code is like:
public string HeadingText
{
set
{
lblTitleContent.Text = value;
}
get
{
return lblTitleContent.Text;
}
}
Now in the page_load() of ContentPage1.aspx set the HeadingText Property like following:
protected void Page_Load(object sender, EventArgs e)
{
DemoMaster master = (DemoMaster)Master;
master.HeadingText = “Content Page #1”;
}
Similarly, in the page_load() of ContentPage2.aspx set the HeadingText Property like:
protected void Page_Load(object sender, EventArgs e)
{
DemoMaster master = (DemoMaster)Master;
master.HeadingText = “Content Page #2″;
}
Now, Run the application, and navigate to the pages, we will note that the Text of lblTitleContent is changed according to the content page navigated.
( Note: Another way to get strongly typed access to the master page is to add the MasterType directive to the content page:
Now we can directly call the property of master Pages like
Master.HeadingText = “Content Page #1”;)