SIGN UP MEMBER LOGIN:    
ARTICLE

Content Page and Master Page in ASP.NET MVC

Posted by Prashant Chaudhary Articles | ASP.NET MVC with C# November 28, 2011
This article will help you to learn how to use the View Master Page to show same content on all the pages in an ASP.NET MVC Application.
Reader Level:

Use of  Master Page in ASP.NET MVC Application

As we know, to develop any application which is a combination of various pages having different functions but some part  remains the same on all the pages of maximum pages hence to overcome the workload of doing same work again and again for all the pages we can use the facility of view master page in MVC .As  in an application their are total ten views and five of them are having same structure and other five are having different structure hence in that case we can use two different master pages and can overcome the workload.

Let's starts with creating the View Master Page in an application in which the user requires the same structure to be used on all pages.

Steps to add Master Page to MVC project :

Right-click the Shared (View) folder

1n.gif

Then select on Add>New Item

After that select the MVC View Master Page template

2.gif

Now if user requirements are different for some pages then he/she can create another master pages as per requirement and can overcome the workload.

Now Code of the Master Page

<%@  Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="MvcApplication10.Views.Shared.Main" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<
html xmlns="http://www.w3.org/1999/xhtml">
<
head id="Head2" runat="server">
   
<title></title>

   
<style type="text/css">

       
html

       
{

           
background-color: gray;

       
}

       
.column

       
{

           
float: left;

           
width: 300px;

           
border: solid 1px black;

           
margin-right: 10px;

           
padding: 5px;

           
background-color: white;

           
min-height: 500px;

       
}

   
</style>

   
<asp:ContentPlaceHolder ID="head" runat="server">

   
</asp:ContentPlaceHolder>

</
head>
<
body>
   
<h1>
        My Website</h1>
    
<div

class="column">

       
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

       
</asp:ContentPlaceHolder>

   
</div>

   
<div class="column">

       
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">

       
</asp:ContentPlaceHolder>

   
</div>

</
body>
</
html> 


3.gif

Now implementation of View Master Page in Content View Page

Step to Create a View Content Page based on master page

  •  Right click on Home (Views) folder
New Picture.bmp
  •  After that Select New Item and select MVC View Content Page
  •  Now enter the name and Click on Add button

4.gif

Code of the View Content Page

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"    CodeBehind="Index.aspx.cs" Inherits="MvcApplication10.Views.Home.Index" %><asp:content id="Content1" contentplaceholderid="head" runat="server">
</asp:content>
<
asp:content id="Content2" contentplaceholderid="ContentPlaceHolder1" runat="server">
</
asp:content>
<
asp:content id="Content3" contentplaceholderid="ContentPlaceHolder2" runat="server">
</
asp:content>

Conclusion: Using master page we can work efficiently, easily and this feature makes working more user friendly while developing an application. 

Login to add your contents and source code to this article
share this article :
post comment
 

its good Prashant

Posted by bharat soni Dec 10, 2011

its good Dear

Posted by bharat soni Dec 10, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor