We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

How to Change Heading, Title of a Master Page depending upon the content page? (Without any additional ContentPlaceHolder )

MasterPages give a consistent look to cur web application. Whole data of  content pages are available inside the ContentPlaceHolder, and the remaining portion of the MasterPage remains consistent.

What if we require to change the TitleText,TitleImage of Master Page (based on our Content Page)?

Here's the solution:

Step 1: Define a public property for MasterPage class that gets/sets the value of Label, Image or any other web control used for displaying Heading of the Page.
 

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:

 <%@ MasterType VirtualPath ="~/DemoMaster.master" %>

 

Now we can directly call the property of master Pages like

  Master.HeadingText = "Content Page #1";)


Related Tags:

ASP.NET

Author: Satyadeep Kumar

top

ASP.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login