Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 256943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:03:21+00:00 2026-05-11T22:03:21+00:00

I have a master page with a content control. I want it to do

  • 0

I have a master page with a content control. I want it to do something specific during load if a particular content place holder has been used.

APageWithContent.aspx

<asp:Content ContentPlaceHolderID="myContent" runat="server">
    <div>hello world!</div>
</asp:Content>

MasterPage.aspx

<asp:ContentPlaceHolder ID="myContent" runat="server" />

It’s optional on the pages that use the master whether they provide any content for this placeholder.

I want to be able to tell during the load and prerender events in the master page whether there is a content for my placeholder on the current page.

MasterPage.aspx.cs

void Page_Init( object sender, EventArgs e )
{
    if( /* myContent is populated */ )
        //do something
}

Anyone know how to discern this?

In WebForms you can check the controls:

myContent.Controls.Count > 0

But for MVC views this always appears to be 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:03:21+00:00Added an answer on May 11, 2026 at 10:03 pm

    I did this by just checking myContent.Controls.Count.. However, you obviously need to take into consideration any default controls that might be added.

    For example, if you have your content placeholder defined as:

    <asp:ContentPlaceHolder ID="myContent" runat="server">
    </asp:ContentPlaceHolder>
    

    Then you will get a LiteralControl in the controls collection containing “\r\n” (since the line break in the definition is parsed).

    So, if you remove that and define as:

    <asp:ContentPlaceHolder ID="myContent" runat="server"></asp:ContentPlaceHolder>
    

    Then you can do the check in the master page load:

    if (myContent.Controls.Count > 0)
    // do stuff when populated with content..
    

    Just make sure you test the code to make sure you are actually checking for content added by the content pages, and not any default content defined in the master.

    UPDATE

    Following edit saying this problem is MVC-only I can confirm I am unable to replicate. I used the same code as above in an MVC app and it worked exactly as expected & described.

    Can you post some sample code of what you have in place?

    FYI – Here is the [working] code:

    Site.Master

    <asp:ContentPlaceHolder ID="myContent" runat="server"></asp:ContentPlaceHolder>
    
    <% if (myContent.Controls.Count > 0) { %>
        <b>Content Added!</b>
    <% } %>
    

    Index.aspx

    <asp:Content ContentPlaceHolderID="myContent" ID="myContent" runat="server">
    This will render and the "Content Added!" will also be rendered.
    </asp:Content>
    
    <%-- This will cause the "Content Added!" to be displayed but with no visual content --%>
    <asp:Content ContentPlaceHolderID="myContent" ID="myContent" runat="server">
    </asp:Content>
    
    <%-- This will NOT render the "Content Added!" since there is zero content between tags --%>
    <asp:Content ContentPlaceHolderID="myContent" ID="myContent" runat="server"></asp:Content>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When a page that uses a master page doesn't have an asp:Content control for
I have an aspx master/content page scenario. The parent page has an IFrame which
I'm new to ASP.NET and want to have an asp:content control for the page
I have a master page which has a content section with the id cpMainContent.
My content page has a master page. In the content page I have specified
I have a master page with Header, Menu, Content and Footer panes in my
In VS 2008, I have an ASP.NET content page having one master page. I
I have a master page called SampleMaster.master and this page contains a Repeater control
I have a Master Page which has an associated css file. On one of
I have a master page in my asp.net MVC project, which has code like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.