Monday, January 18, 2010

How to Create a ASP.NET Site using Master Pages

Lets begin with a example.

image   This is a typical site layout that you might need to  your web site. Imagine that you are going to create a this type of layout for your pages. In the traditional way you need to create this layout for your every pages. Then imagine  if you need to change it later .. oops.. then you  need to change all the pages you have done.

 

But if you use master page concept it is much more easy, because you can only design the content while keeping the others static (Of cause you can also change the others too. )

In the run time master page and content are merged and will display the page.

image How to do this

STEP 1 : First of all you need a Web Project or a Web Application. Thus Open the visual studio and go and create a new web project.

STEP 2: Then you need to add a master page to the project . thus Goto –> Add New Item and select the master page and click add button.

image

STEP 3 : The Design your master page.





  1. <head runat="server">
  2.     <title></title>
  3.     <asp:ContentPlaceHolder ID="head" runat="server">
  4.     </asp:ContentPlaceHolder>
  5. </head>
  6. <body>
  7.     <form id="form1" runat="server">
  8.     <table>
  9.         <tr>
  10.             <td colspan="3">
  11.                 Header</td>
  12.         </tr>
  13.         <tr>
  14.             <td rowspan="2">
  15.                 Menu</td>
  16.             <td rowspan="2">
  17.                     <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  18.                     </asp:ContentPlaceHolder>
  19.             </td>
  20.             <td >
  21.                 Banner</td>
  22.         </tr>
  23.         <tr>
  24.             <td >
  25.                 Adverticement</td>
  26.         </tr>
  27.         <tr>
  28.             <td colspan="3">
  29.                 Footer</td>
  30.         </tr>
  31.     </table>
  32.     </form>
  33. </body>
  34. </html>



Step 4 : Now you want to add a content page (web form)  to the project. thus GOTO –> add new item and select web form. In here don’t forget to check the select master page. It will list down available master pages and you should select what you want. Then It will automatically generate code segment to place your content.

image

image

Step 5: Now you can code your content page.





  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5.     <div>Place your content here</div>
  6. </asp:Content>




Post a Comment