Pages

Wednesday, November 5, 2014

Using Master and Content Pages in ASP.NET 4

Introduction

Master and content pages make it convenient to replicate controls across multiple pages.  They essentially allow you architect the framework of our website and then plug in your content via an ASP.Net Content Placeholder Control.  

Scenario

For example, let's say that you are creating three pages, home page, request login page, and an About Us page. Each page uses the same set of controls to access their information.  



Rather than replicating the code to each page for each button, along with the code behind event handlers, you can create a master page holding the common details.  Then using an ASP.NET Content Placeholder control map the content specific back to the master.

Master Page Example

 

 Creating a Table with two rows to hold ALL the content.

The Master page uses the two row table with the second row holding the place holder for any content provided on a content page.  

The Master page contains the following directive:



The first content control is for the head and is in the form of:







The second control is for the identifies where the content will be placed and looks like this:

 

 As can be seen it is on the second row of the table.

Content Pages Example




















The content above is the only information in the content page, after the page is rendered the output looks as follows:



 

Using a standard code behind file to set the Label control's text property you can generate the content.
The highlighted area in the content page identifies the content control in the master page where this content's page will populate. 


In the case of all three content pages, they will use the same exact content place holder control.