03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Consistent Page Layout with Master Pages x 209<br />

<strong>VB</strong>.<strong>NET</strong><br />

<br />

C#<br />

<br />

To create regions that content pages can fill <strong>in</strong>, you def<strong>in</strong>e ContentPlaceHolder controls <strong>in</strong> your<br />

page like this:<br />

<br />

<br />

You can create as many placeholders as you like, although you usually need only a few to create a<br />

flexible page layout.<br />

The content pages, which are essentially normal <strong>ASP</strong>X files, without the code that they’re go<strong>in</strong>g to<br />

take from the master page, are connected to a master page us<strong>in</strong>g the MasterPageFile attribute of<br />

the Page directive:<br />

<strong>VB</strong>.<strong>NET</strong><br />

<br />

C#<br />

<br />

The page-specific content is then put <strong>in</strong>side a Content control that po<strong>in</strong>ts to the relevant<br />

ContentPlaceHolder:<br />

<br />

<br />

Note that the ContentPlaceHolderID attribute of the Content control po<strong>in</strong>ts to the<br />

ContentPlaceHolder that is def<strong>in</strong>ed <strong>in</strong> the master page. Right now it po<strong>in</strong>ts to the default<br />

name of ContentPlaceHolder1, but <strong>in</strong> a later exercise you see how to change this.<br />

At run time, when the page is requested, the markup from the master page <strong>and</strong> the content page are<br />

merged, processed, <strong>and</strong> sent to the browser. Figure 6-1 shows a diagram of the master page with<br />

just one ContentPlaceHolder <strong>and</strong> the content page that results <strong>in</strong> the f<strong>in</strong>al page that is sent to the<br />

browser.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!