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