Friday, 28 December 2012

Mater Page

Srinivas Amrutha:
In this article, I am going to explain how to create a "Master Page" , what is the Advantages of Master Page, Interview Questions and Answers.

Master Pages:
A mater page can be created to hold those page elements that represents the common look and feel of website. Various page elements that you might normally have added to each content page (banners,footers,menus etc..) can instead be placed in the master page. When a new content page is created, the developer simply needs to link it to an existing master page.
            However, a Master Page can contain a special type of control, called a ContentPlaceHolder control. A ContentPlaceHolder defines a region of the master page rendering that can be substituted with content from a page associated to the master. A ContentPlaceHolder can also contain default content, just in case the derived page does not need to override this content.

To create a Master Pages open  Visual Studio 2010 Click on Start new project ->



  Select ASP.NET Empty web application.



Give the name to project as you like. I give here to my project name as " Masterpage ". Next click on OK Button. Now right click on project and choose "Add New Item", and select the "Master Page"  item.
Select Master Page, give it a file name and click Add



When you create a new master page in Visual Studio, you start with a blank page that includes a ContentPlaceHolder control. If you add a header, that header appears in every content page.
If you want to give the content page the opportunity to supply content in a specific section of
the page, you need to add a ContentPlaceHolder.



Master Page Code:
---------------------------------------------------------------------------------------------------------------
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="srinivas.master.cs" Inherits="Masterpage.srinivas" %>

<!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 runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h2> Wlecome To Srinivas Amruthas's Blog</h2>
    <br />
    <a href="http://dotnettutorialblog.blogspot.in/" target ="_blank" >
    http://dotnettutorialblog.blogspot.in/ </a>

</div>
 
       <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
   
    </form>
</body>
</html>

-------------------------------------------------------------------------------------------------------------------------------------

 Now that i have my Master Page defined, I can go ahead and build pages using it. To build one, right click on project and choose "Add New Item", and select the "web Form using Master Page"  item.



Developer that you want to have this new page use a Master Page. when you click the "add" button it will then ask you to pick the Master Page to use:

Click on OK button. Now, the web form will have the Master page entry as below:

<%@ Page Title="" Language="C#" MasterPageFile="~/srinivas.Master"
AutoEventWireup="true" CodeBehind="Homepage.aspx.cs" Inherits="Masterpage.Homepage" %>


After selecting the master page, on the web form Visual web developer has automatically added an <asp:content> control for the "Main Content" ContentPlaceHolder. 

Content Page:



 Content Page Code:
----------------------------------------------------------------------------------------------------------
 <%@ Page Title="" Language="C#" MasterPageFile="~/srinivas.Master"
AutoEventWireup="true" CodeBehind="Homepage.aspx.cs" Inherits="Masterpage.Homepage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h3 > Hi This is Srinivas Amrutha</h3>
</asp:Content>

 --------------------------------------------------------------------------------------------------------------
Debug content page:


Advantages of Master Pages:

If you're wandering why you should start using master pages take a look at the following list:
  • Creating websites using master pages usually requires less work because you create a template of website and then just add content to it.
  • Consistent look and feel - because there is only one master page each webpage has exactly the same design.
  • It's flexible - if you want to change something in look and feel of your website you can just edit master page, then each of your pages will also be changed.
  • You can use pages designed for content placeholders also in you other projects. For example you can use contact page on various websites with different designs because it usually remains the same.
  • There are mechanisms that allow you to change master page from each content page.
  • It's easy to change final layout of your pages.
Interview Questions: 

1. What is the file extension for a master page?
 .master
 2. Can I write any script outside Content control of the content page?
No, if you write any script outside of Content tag in a content page, you will get a compilation error. 
Compilation Error: “Only Content controls are allowed directly in a content page that contains Content controls.”   
3. Can I access controls of master page in content pages?
Yes, controls in master page are accessible to content pages.  
4.  Can I add more than one content placeholder to a master page? 
Yes, a master page can have more than one ContentPlaceHolder.  
 5. How do you identify a master page?
The master page is identified by @Master directive, which replaces the @Page directive that is used for    ordinary aspx page.  
 6. Can master page inherits another master page..?
Yes.One master page can inherit a second master page in ASP.NET.
 However ,a single page cannot inherit two different master pages directly. But a single page inherit two different master pages indirectly.
7. Define multiple Master Pages..?
In ASP.NET, you can have multiple master pages each for a different purpose. You can provide users several layout options using Multiple Master Page. You can define Master Page at multiple places in the web application.
You can specify page-level using the @Page dierective.
You can specify using the Web.config.
Remember that the definition closest to the user wins that means page-level definition supersedes site-level definition.  

8. What is a content placeholder..ContentPlaceHoder is a region where replaceable content will appear.  
9. At what stage of page processing, master page and content page are merged?
Master page and content page are merged during page initialization stage.  
10. What are the different ways to attach content pages to a master page?
You can attach content pages to a master page at three levels: 
 I. page level
<%@ Page Language="C#" MasterPageFile="MySite.Master" %>
 II. Application Level: By making a setting in the pages element of the application's configuration file
<pages masterPageFile="MySite.Master" />
 III. At the folder level : This strategy is like binding at the application level, except that you make the setting  in a Web.config file in one folder only.  


No comments:

Post a Comment