Powered by Blogger.

Thursday, September 26, 2013

Master Page in Asp.Net

To create web pages with a consistent layout you need a way to define these relatively static regions in a single template file. Versions of ASP.NET prior to 2.0 did not have a template solution so you were forced to duplicate your page layout on every single page in the web site.Fortunately, this is no longer the case due to master pages.

Also you can check out my previous posts on:

- Shortcut keys in Visual Studio 2010

- Filter Attribute in MVC

- Functions and Subroutines in C#.Net

The biggest benefit of master pages is that they enable you to define the look and feel of all the pages in your site in a single location.

This means that if you want to change the layout of your site i.e, if you want to move the menu from the left to the right,you only need to modify the master page and the pages based on this master will pick up the changes automatically.

To some extent, a master page looks like a normal ASPX page. It contains static HTML such as the <html>, <head>, and <body> elements, and it can also contain other HTML and ASP.NET server controls.

A master page uses an @ Master directive that identifies the file as a master page.


<%@ Master Language="VB" %>


<%@ Master Language="C#" %>

The content pages, which are essentially normal ASPX files, but without the usual code you find in them like the <html>, <head>, <body>, and <form> elements, are connected to a master page using the MasterPageFile attribute of the Page directive:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/Test.master"
AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default">

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Test.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default">

The page-specific content is then put inside a Content control that points to the relevant ContentPlaceHolder:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"

A master page is useless without a content page that uses it. Generally, you’ll only have a few master pages, whereas you can have many content pages.