Hire my services

If you would like to hire my services, you can now do so by visiting the following link:

Website Design Darlington

Article Statistics

ArticlesLatest Entry
2121-May-2008

Code Bank Statistics

CategoryTipsLatest Entry
Applications420-Feb-2008
Cache118-Apr-2007
Controls926-Jul-2007
CSS107-Sep-2007
Database428-Aug-2007
Dates128-Aug-2007
Email105-Mar-2007
Files123-Apr-2007
HTML101-Mar-2007
Images226-Mar-2007
Pages115-Oct-2007
Sessions122-Nov-2007
SQL Server1518-Mar-2008
Strings309-May-2007
Skip Navigation LinksHome > Articles > Access Master Page controls from the Content Page

Access Master Page controls from the Content Page

Summary: An example of how you can get or set the values of a control on the Master Page from one of your content pages.Socialize it

One of the questions that has been cropping up on the forums a lot recently, is how to interact with with controls on the master page from one of your Content Pages.

There are a couple of ways I can think of to do this, but the method I prefer is to use Properties. To demonstrate how we create and use these Properties, imagine we have a Master Page which contains a Label control (we'll just call it "Label1"), and we want to show it's value in a TextBox on our Content Page. Our Master Page, would look like this:
and our Content Page would look like this:

Now, we need some way of getting the Master Page to expose the Text property of it's "Label1" control. This is where the Properties come in and to set this up we need to make the Property Public so that it can be accessed by other pages. So, our code behind for the Master Page will now consist of:
You'll notice that there is both a Get and a Set method for this Property which will allow us to both retrieve and update the value. To do this from our Content Page, we first need to get a reference to the Master Page, which can be done easily by using the Page.Master property, and then we need to cast the Master Page to the type of Master we are using so that we can see the Public Property and Get the value. We can also use the same methods to Set the property, as is shown by looking at the code behind for the Content Page:

When you run this code in your development environment, you'll see that the first Label shows our "Changed" text, whereas the TextBox will show the previous value which we retrieved via the Get method.

This article shows you a simple example of how to interact with controls on the Master Page, and will give you the necessary logic to build on this code and apply it to similar situations where you need the interaction between the two pages.

Reader Comments

1. Fuquey 18/02/2008 21:02:01

2. burghboy 26/03/2008 16:23:09

3. jinal 06/08/2008 06:00:30

Add your comments

Please keep your comments relevant to this page. Any inappropriate or purely promotional comments may be removed. Email addresses are never displayed but are required so you can confirm your comments.

Your Name:
 
Your Email:
 
Add your comments: