.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’.
Your Default Web Content page will look like this:
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:
And your Web Content page to look like this:
Now you can be loud and proud:
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.