Wednesday, May 16, 2007

Using Master Pages in Web Applications

Master page helps you to create a consistent look and feel to your site while minimizing the amount of work you have to do. With the introduction 2.0 Master Pages, you no longer have to use server side includes to keep the site consistent among different pages. To be able to use and apply master page to your site you first need to create a master page:

1. Right click on the Project
2. Click Add New Item
3. Change the filename as appropriate then click OK to accept
4. Design the layout of the page as you wish and save it

Note that the design in the master page will be applied to all pages that use it except for the content part.

Now that we have a master page, we can start using it. Let's create a web page that would use the master page.

1. Right click on the Project
2. Click Add New Item
3. Select Web Form
4. Check the box called Select Master Page
5. Select the master page you just created and hit OK.

At this point, the code window will open up for you to edit. The new web page doesn't look like a typical web form but instead it will look something like this:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
your code goes here
your code goes here

When you run the application and/or browsing to the web form, IIS will merge both the master page and the web form to produce the finale code and send it to the browser.

Now that you know how to use master page, you might want to take a look at this blog to learn how to dynamically change master page at run time

No comments: