How to convert an ASP.NET 2 page to use MasterpagesIf you have made several ASP.NET 2 webpages, and you decide that you want to convert them to use MasterPages, how do you go about it? There are plenty of tutorials on the web about how to create pages from scratch, using MasterPages, but I could find no advice on how to convert existing pages to use a master page. (BTW, the pages I'm talking about were made in ASP.NET 2 in Visual Web Developer Express, using c# code-behind pages.) So, this page describes the technique I worked out to do this. First, what exactly are Master Pages?Master Pages are used to give all the webpages on your website a standard look
and feel, such as a standard header, menu system, footer etc. All the standard
elements are placed on the Master Page, which also contains a slot for the content
page. Each content page is then framed within the Master Page so that all the
standard elements of the master page appear on each page. See the diagram for
how it is presented in Visual Web Developer. Adding a Master Page to an existing web pageThe first step is to create your Master Page. To do this:
Now examine the source code of one of your content pages. In the first line you will find:
You need to add these two new parameters into that line:
If the content page contains this line:
then delete it, as the Master Page also contains this line. The Master Page contains all the HTML codes for the <head> and starting and ending the <body>, so you must remove all the following top part of your content page:
and this ending section: The main content of your page will now be contained in just a <div>...</div> tag, so wrap it up inside a Content component using these lines of code:
and
It should now look something like this:
You have now converted a content page to use a Master Page. It is not too difficult, eh? How to create a Master Page from scratchIf you already have a Master Page and you want to create a page from scratch to use it, then things are much more simple:
|