Master Page Content Not Visible Visual Studio 2008

.NET For The Iodine-Deficient

I have noticed that my posts on Ultra-Simple errors and topics (e.g. JQuery Toggle Div and Regex For Alphanumeric Plus Punctuation) get quite a few hits so I assume that many of you out there, like me, are Iodine-Deficient and do not read documentation.

So let’s wipe the drool from our imperfectly-shaven chins and proceed with solving possibly the simplest Master Page File bug imaginable.

Creating Master Page and Content Page in VS2008

Create the Master Page by right-clicking the Project and going Add ->New Item -> Master Page
Create the Content Page by Add->NewItem->Web Content Form and select the Master Page File you just created when the dialog appears.
N.B. Do NOT use a Web Form, use a Web Content Form.

Web Content Forms are aspx pages which contain only Content Controls. This is what you need with Master Pages. If you try to use a normal Web Form with a Master Page you will probably end up with the error ‘Content Control must be a top-level control’ (unless you hack the Web Page markup).

Create Some Master Page Content

The Master Page you just created will contain two content areas, one in the head section of the page and one in the body. Type some text in the one in the body such as ‘I am Iodine-Deficient’.

vs2008masterfile<

Your Default Web Content page will look like this:

vs2008contentpage<

Now run your project.
Your Web Content page DOES NOT show the text you just typed into the Master Page.

Where Is My Content ?

The asp:ContentPlaceHolder control in the Master Page contains the default content for the asp:Content control in the Web Content pages with the matching ContentPlaceHolderId. The content within the asp:Content control on any Web Content page, is page-specific and takes precedence over what is in the asp:ContentPlaceHolder control on the Master Page.

As Scott Mitchell explains in more technical language:

Content pages include a Content control for each of the master page’s ContentPlaceHolder controls. When the content page is visited through a browser the ASP.NET engine creates the master page’s control hierarchy and injects the content page’s control hierarchy into the appropriate places. This combined control hierarchy is rendered and the resulting HTML is returned to the end user’s browser.

The empty content in the asp:Content ID=ContentPlaceHolder1 control on the Web Content page is injected into the place occupied by ContentPlaceHolder1 on the Master Page That’s why the text ‘I am Iodine-Deficient’ in the Master Page is not emitted when the Content Page is rendered.

Making The Master Page Content Visible

If you want the text “I am Iodine-Deficient’ to appear on every page in your app., put it outside the asp:ContentPlaceHolder controls.

Amend your Master Page to look like this:

iodineintherightplace

And your Web Content page to look like this:

slowlyrecovering

Now you can be loud and proud:

muchbetter

You can also make Master Page content get shown on selected Web Content Pages by deleting the matching Content control from the Web Content Page. That way, whatever’s in the Master Page ContentPlaceHolder will get rendered on the Web Content Form because there’s no page-level content to override it.

Further Information

Lugol’s Solution available from Microsoft’s Master Page tutorial on their Learn ASP.NET site.

Advertisements

Tags: ,

12 Responses to “Master Page Content Not Visible Visual Studio 2008”

  1. Heartburn Home Remedy Says:

    I can tell that this is not the first time you mention this topic. Why have you chosen it again?

  2. Piyush Says:

    Thanks a lot for the suggestion.
    I was very much upset because my master pae would not work but now because of your halp its working.

    Thanks Alot

    • baraholka1 Says:

      Hiya Piyush,

      Glad to be of assistance.
      Remember, Bite a Wax Tadpole once a day for optimum health and well-being.

      Best Regards,

      baraholka

  3. Gwen Says:

    thank’s that was great … finally my content appear

    • baraholka1 Says:

      Gwen,

      Very Glad my Journey Of Pain has assisted you.
      Bite The Tadpole again sometime soon.

      Regards,

      Barra

  4. Abhilash kumar mohan Says:

    Thanks a lot…this post helped me to solve my design issues of vs2008.

  5. Usman Ahmad Says:

    Thanks buddy your post has saved my time a lot.

    • baraholka1 Says:

      Usman,

      Well Tadpoled. And may your erstwhile namesake, now batting at #3 for Australia make a triple ton
      in his next 57625635 Tests, consecutively.

      Rgds,

      Barra.

  6. shakeer Says:

    Thanks, Good explanation

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


%d bloggers like this: