This tutorial will show how to use Master Pages in Visual Studio, and the ability to dynamically change them.

Master Pages allow you to create definitive styles for your web pages, and they have a higher precendence than CSS Stylesheets. Furthermore, Visual Studio makes it easy to dynamically change the look and feel of your web page using Master Pages.

Add New Item to website, choose Master Page.

Create layout table for Master Page, and add Content Placeholder control from toolbox. This is where dynamic content will be displayed on the Master Page. Modify the page to your liking (color, etc.)

Note the @ Master instead of the usual @ Page declaration.

Add a New Item to website; Web Form. Check Select master page, and choose the Master Page you just created.
When in Design View, your new page will appear with the Master Page in the background. You can make changes to your new page where the Content Placeholder is.

To change the Master Page dynamically:

Create a new Master Page different from the first. Then add buttons to change between the Master Pages.

Note this code is in the Page_PreInit section of the Web Form(s), which will be merged with the Master Page.

The front end pages should look something like this:



Download Source Files