Monday, October 27, 2008

Working with Master Pages and Basics of Master Pages

Working with Master Pages and Basics of Master Pages
Why do you need master pages and how it can improve the maintenance of your website

Introduction

Master Pages is great new enhancements in ASP.NET 2.0 and if you haven't heard or use Master Pages, then you might reconsider of using this across all your existing websites. It will save you lots of times in maintaining and editing your websites source code. In old classic ASP, we do not have this features yet, and lots of people actually create their own Master Pages by using template page that is inherited or include on all their page.
They normally will copy and paste this tag on all their pages.



The problem with this was that you had to take into account the newly opened HTML tags in the header include file. These tags had to be closed in the main document or in the footer include file. It was usually very difficult to keep all the HTML tags in order especially if multiple people worked on a same web projects. Web pages sometimes display strange results because of inappropriate or non existent tag closings or openings.


With the introduction of ASP.NET 1.0, developers started using user controls to encapsulate common sections of their Web Pages. For instance, you could build a Web page that include header,navigation and footer sections by simply dragging and dropping these sections onto each page that required them.

This technique worked, but it also raised some issue as the include files in Old Classic ASP file. This is because User Control actually will be rendered the same as include files in Classic ASP.

In ASP.NET 2.0, Master pages has been introduced and this has make all the developers life easy. You can design and see the master pages visually in your Visual Studio 2005.

Why Do You need Master Pages

Most Websites today have common elements used throughout the entire application or on majority of the pages within the applications.
For instance if you look on WorldOfASP.NET website (http://www.worldofasp.net), you can see that the header and footer is being repeated across all the pages in entire website.

Nah, those repeated elements can be stored in the Master Pages. This will save you lots of times to copy and paste the code on all the pages. And imagine if you want to edit the header on footer, then you only need to edit on one single place rather than have to find and replace across all your files.

How to Use Master Pages

Master Pages are an easy way to provide a template that can be used by any number of ASP.NET pages in your application. In working with Master Pages, you create a master file that is the template referenced by a subpage or content page. Master Pages use .master file extension whereas content pages use .aspx file extension.

To create Master Page, it will be very easy and simple in Visual Studio 2005.
Right Click on your Project and Choose Add New Items --> Choose MasterPage as VS Template
Give the MasterPage a name e.g MasterPage.master.




After you added the Master Page, you will see the directive on top of the page just like below.

<%@ MASTER LANGUAGE="C#" AUTOEVENTWIREUP="true" CODEFILE="MasterPage.master.cs" INHERITS="MasterPage" %>

As you can see that the masterpage contain Master directive while normal Page file will have Page directives. The MasterPage behaves just like normal Aspx page, where you can add code behind, Page_Load Event and etc.

In the Master Page, there is also a piece of code like this

 <ASP:CONTENTPLACEHOLDER ID="ContentPlaceHolder1" RUNAT="server">

Web Form
and check the Select Master Page option. You will then be given choice to add Master Page. Choose MasterPage1.Master as the MasterPage.




Your Page file should contain the code like this

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
podcast directory
Bloglisting.net - The internets fastest growing blog directory

RSS Feed

Blog Directory & Search engine

Followers

About Me