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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s