5 February 2014

Master Pages

        ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.
             
       Master pages actually consist of two pieces, the master page itself and one or more content pages.A master page is an ASP.NET file with the extension .master (for example, MySite.master) with a predefined layout that can include static text, HTML elements, and server controls.
              The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages. The directive looks like the following.

Syntax:

C# :< %@ Master Language="C#" %>
VB.NET:<%@Master Language="VB" %> 

The @ Master directive can contain most of the same directives that a @ Control directive can contain. For example, the following master-page directive includes the name of a code-behind file, and assigns a class name to the master page

         <% @Master Language="C#" %>
          <! DOC TYPE html public ">
          <html xmlns="http://www/w3.org/1999.xhtml">
          <head runat="server">
         <title>Master Page title</Master page>
         </head>
         <body>
      <form id="form1" runat="server">
      <table>
         <tr>
             <td><asp:contentplaceholder id="Main" runat="server"/></td>
              <td><asp:contentplaceholder id="Main" runat="server"></td>
          </tr>
      </table>
     </form>


Content Pages:

        You define the content for the master page's placeholder controls by creating individual content pages, which are ASP.NET pages (.aspx files and, optionally, code-behind files) that are bound to a specific master page. The binding is established in the content page's @ Page directive by including a Master Page File attribute that points to the master page to be used. For example, a content page might have the following @ Page directive, which binds it to the Master1.master page

<% @ page language-"C#" MasterPageFile="~/MasterPages/Master1.master" title="Content Page" %>   
      After creating Content controls, you add text and controls to them. In a content page, anything that is not inside the Content controls (except script blocks for server code) results in an error. You can perform any tasks in a content page that you do in an ASP.NET page. For example, you can generate content for a Content control using server controls and database queries or other dynamic mechanisms

<asp:Content Id="Content1" ContenPlaceHolderID="Main" runat="server">
    //Main Content//
</asp:Content>

 <asp:Content Id="Content2" ContenPlaceHolderID="Footer" runat="server">
    //Main Content//
</asp:Content> 

Advantages of Master Pages

        Master pages provide functionality that developers have traditionally created by copying existing code, text, and control elements repeatedly; using frame-sets; using include files for common elements; using ASP.NET user controls; and so on. Advantages of master pages include the following:
·         They allow you to centralize the common functionality of your pages so that you can make updates in just one place.
·         They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
·         They give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls are rendered.
·         They provide an object model that allows you to customize the master page from individual content pages. 

Runtime Behavior of Master Pages    

At run time, master pages are handled in the following sequence:

1.    Users request a page by typing the URL of the content page.
2.    When the page is fetched, the @ Page directive is read. If the directive references a master page, the master page is read as well. If this is the first time the pages have been requested, both pages are compiled.
3.    The master page with the updated content is merged into the control tree of the content page.
4.    The content of individual Content controls is merged into the corresponding Content Placeholder control in the master page.
     5.    The resulting merged page is rendered to the browser..

At the page level: You can use a page directive in each content page to bind it to a master page

At the application level: By making a setting in the  pages  element of the application's configuration file (web config),you can specify that all asp.net pages(.aspx files) in the application automatically bind to a master page 

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. The master-page bindings then apply to the ASP.NET pages in that folder.


No comments:

Post a Comment