How-To Access Methods From MasterPage With No CodeBehind File

Create a new MasterPage called “MasterPage1.master” unchecking “Place code in separate file”

At the top of the new MasterPage in the page directive append a ClassName property:

<%@ Master Language="C#" ClassName="MasterPage1" %>

In the script section add your public method:

<%@ Master Language="C#" ClassName="MasterPage1" %>

<%@ Register Src="../uc/footer.ascx" TagName="footer" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    public void SetMastHeadImage(string ImageUrl, string AlternateText)
        this.imgMastHead.ImageUrl = (string.IsNullOrEmpty(ImageUrl)) ? "" : ImageUrl; ;
        this.imgMastHead.AlternateText = (string.IsNullOrEmpty(AlternateText)) ? "" : AlternateText;

Create a new Page selecting the Master Page you just created.

Add code to invoke the method you just added:

<%@ Page Title="" Language="C#" MasterPageFile="~/mp/MasterPage1.master" %>

<script runat="server">
    protected override void OnLoad(EventArgs e)
        // Define mast head image / alt text             
        ((MasterPage1)Master).SetMastHeadImage("../images/aboutus/masthead-AboutUs.jpg", "About Us");


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s