How To Test If MasterPage Content Place Holder Has Content Or Is Empty

protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
      
        DivContentAboveBottomMasthead.Visible = (HasNonEmptyControls(CphAboveBottomMasthead)) ? true : false;       
        
    }

    public static bool HasNonEmptyControls(ContentPlaceHolder cph)
    {
        if (cph.Controls.Count == 0)
        {
            return false;
        }
        else if (cph.Controls.Count == 1)
        {
            LiteralControl c = cph.Controls[0] as LiteralControl;

            if (string.IsNullOrEmpty(c.Text) || IsWhiteSpace(c.Text))
                return false;
        }

        return true;
    }

    private static bool IsWhiteSpace(string s)
    {
        for (int i = 0; i < s.Length; i++)
            if (!char.IsWhiteSpace(s[i]))
                return false;

        return true;
    }

Source

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

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");

    }
    
</script>


How To Properly Handle MasterPage Updates from Pages

The better way to handle updates to the MasterPage from pages is to use Set/Gets (Properties) within the MasterPage. This makes the solution more resilient and it encapsulates.

// MasterPage

public partial class MasterPage : System.Web.UI.MasterPage
{
public string HeadingLabelText
{
get { return HeadingLabel.Text }
set { HeadingLabel.Text = value }
}

Than to use this new property from within the Page go into aspx source of page and after the page directive, adding another directive: MasterType. Adding this MasterType directive gives VS.NET and ASP.net the intelligence to know where to pick up the appropriate class to which has the property we are interested in (the one we just added: HeadingLabelText), in the Master object within the c# code behind. Without this when you type Master. The appropriate property “HeadingLabelText” won’t be listed in intellisense and ASP.NET won’t recognize the property when it renders the page.

// Page

<%@ Page Title……. %>
<%@ MasterType VirtualPath=”~/MasterPage.master” %>

Than add the following to Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
Master.HeadingLabelText = “Retailer Website – “Products”;
}