Posted by: Cirilo Meggiolaro | 10/21/2008

Tip of the day #7 – Accessing ASP.Net master page properties from within content pages

Do you know there is an easy way to access master pages properties within content pages in ASP.NET? It’s nice, but if I tell you that you can have access and count on intellisense’s help?

It’s quite simple. Two main steps and you’ll be able to access it:

1. In your master page code behind, create a property like any other. In this example, I have created one called MyMessage that gets or sets the text property from a label control named lblMessage:

public string MyMessage

{

get { return lblMessage.Text; }

set { lblMessage.Text = value; }

}

2. In your content page aspx file, add the following page directive. In this example, my master page class name is called MyMasterPage:

<%@ MasterType TypeName=”MyMasterPage” %>

3. Your master page is not anonymous in design time anymore. You are telling your content page the master page type to use. Now it is straightforward. From your application’s content page code you have full access to the property (with intellisense) just typing Master plus the property name like in the code below:

Master.MyMessage = “Hello Master!”;


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

Categories

%d bloggers like this: