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.

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"
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top