Geekpedia Programming Tutorials






Accessing the controls and methods of a MasterPage

If you are looking to access the method, property or control of an ASP.NET MasterPage from inside a web form you need to do a little trick to get it to work properly. This tutorial explains how you can do this with only a few lines of code, and it also shows you how to create MasterPages that you can use with web forms.

On Sunday, May 7th 2006 at 11:34 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 18 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
Download this Visual Studio 2005 project Download this project (Visual Studio 2005)

It won't take you long to use MasterPages and feel the need to call a method or a property of that MasterPage class from inside a web form. The typical instinct is to use Master.MethodName() inside the ASP.NET web form file. Unfortunately that will not work... fortunately there is a an easy fix. First we'll quickly review how to create the MasterPage and the web form, then we'll move on to accessing the MasterPage's method.

How to create a MasterPage to use with a Web Form

Start by creating an ASP.NET 2.0 Web Site project in Visual Studio 2005 and besides the already existing Default.aspx file, add a MasterPage entitled DefaultLayout.master file in the same directory. Replace all the content of our new DefaultLayout.aspx file so that it contains the following:


<%@ Master Language="C#" AutoEventWireup="true" CodeFile="DefaultLayout.master.cs" Inherits="DefaultLayout" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Sample Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h1><asp:Label runat="server" ID="lblHeading"></asp:Label></h1>

        <asp:contentplaceholder id="MainPageContent" runat="server">

        </asp:contentplaceholder>

    </div>

    </form>

</body>

</html>


As you can see, we placed a label lblHeading and a placeholder entitled MainPageContent. The label will be the one that will be assigned a value to from inside the Default.aspx, through a method inside the MasterPage. The placeholder is there just to give a reason for the MasterPage; we will set it with a some content inside Default.aspx.

Now let us create a (very simple) method inside DefaultLayout.master:


public void SetPageHeading(string PageHeading)

{

   lblHeading.Text = PageHeading;

}


This is the method we're going to access from inside Default.aspx's code-behind file. The parameter passed (PageHeading) is the one to which the label gets assigned.

Now switch to the Default.aspx page and inside the code view replace all its current content with the following:


<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="DefaultLayout.master" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:content id="HomePageContent" contentplaceholderid="MainPageContent" runat="server">

This phrase is placed in a content placeholder inside the Default.aspx page which uses the DefaultLayout.master MasterPage.

</asp:content>


With this markup we are setting the MasterPage to be used with this Web Form, and the content of the placeholder.

Accessing the methods of a MasterPage from inside the Web Form

What's left is setting the label of the MasterPage through code, thus switch to code view and inside the Page_Load event use the following piece of code:


((DefaultLayout)this.Master).SetPageHeading("This heading is set from inside Default.aspx");


This is the most important line of the whole tutorial, it casts the this.Master object to DefaultLayout which is the type of the MasterPage (remember we named the MasterPage DefaultLayout). After we do the casting, the method is accessed normally, as any other method, with the parameters passed inside parenthesis.

We used the label changing example method just to prove how you can access a MasterPage's method from inside a web form, however if the only thing you are looking to do is to change the value of a label or access the properties of a similar control, you should have a look at the following:

Accessing the controls of a MasterPage from inside a Web Form

If you wish to access a control that's inside the MasterPage, you need to use the FindControl() method as shown below:


Label lblHeading;

lblHeading = (Label)Master.FindControl("lblHeading");

lblHeading.Text = "This heading is set from inside Default.aspx";


And here is the same version of the code, this time in VB.NET:


Dim lblHeading As Label

lblHeading = CType(Master.FindControl("lblHeading"), Label)

lblHeading.Text = "This heading is set from inside Default.aspx"

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by shyam on Monday, May 15th 2006 at 10:30 AM

Information is very useful for us but u have to send more detail about ur proj

by Ashwani Kumar on Wednesday, May 24th 2006 at 01:59 AM

I like to use this site frequently its an very usefull for the biggners to learn any language such as for web technology.

by Encantado on Tuesday, October 2nd 2007 at 04:51 AM

These articles have been written in a rush, they have no substance and are purely there to try and boost your site popularity. If you want credability, you need to give the subject a full or nearly full treatment, a student in 11th grade could write this stuff!

Sorry guys, but there you go.

PS : On the plus side, nice looking site

by Grady Christie on Tuesday, July 14th 2009 at 02:08 PM

Thanks. This was very helpful. Grady Christie

by CHarl on Sunday, August 23rd 2009 at 08:01 AM

Hi there.
I am an aspiring .net developer still at the beginner level and this single bit of information has been the most informative I've found so far.
Thank you so much, very useful!

by Amit Mathur on Friday, September 18th 2009 at 09:39 PM

The information is not very helpful as since I am not able to obtain the result through it and it resulted in an error. INstead it need some classname to be includede before anything may happen..

by bvbb on Tuesday, December 1st 2009 at 01:50 AM

fdbjkhnmvm,vm,m


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons