How To Properly Handle MasterPage Updates from Pages

The better way to handle updates to the MasterPage from pages is to use Set/Gets (Properties) within the MasterPage. This makes the solution more resilient and it encapsulates.

// MasterPage

public partial class MasterPage : System.Web.UI.MasterPage
{
public string HeadingLabelText
{
get { return HeadingLabel.Text }
set { HeadingLabel.Text = value }
}

Than to use this new property from within the Page go into aspx source of page and after the page directive, adding another directive: MasterType. Adding this MasterType directive gives VS.NET and ASP.net the intelligence to know where to pick up the appropriate class to which has the property we are interested in (the one we just added: HeadingLabelText), in the Master object within the c# code behind. Without this when you type Master. The appropriate property “HeadingLabelText” won’t be listed in intellisense and ASP.NET won’t recognize the property when it renders the page.

// Page

<%@ Page Title……. %>
<%@ MasterType VirtualPath=”~/MasterPage.master” %>

Than add the following to Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
Master.HeadingLabelText = “Retailer Website – “Products”;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s