Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 

YOU ARE HERE: HOME Articles What are Master Pages in NET Why we need Master Pages and how do we use Master Pages in our



What are Master Pages in .NET? Why we need Master Pages and how do we use Master Pages in our applications?

View(s): 13119


What are Master Pages in .NET? Why we need Master Pages and how do we use Master Pages in our applications?


Life before Master Pages

For a given website, there are multiple web pages with common layout. How we can achieve this?

  • Write the layout of the code in each page. But this leads to code redundancy which is not correct.
  • We can achieve the common layout, by using User controls.

Advantage of User Controls:

  • Turning an existing ASP.NET page into a user control requires only a few minor changes. User controls can be easily linked to any page that needs their services.
  • Furthermore, changes to a user control's implementation do not affect the referencing page and only require recompiling of the user control into an assembly.

Disadvantage of User Controls:

  • Any alteration to the control's public interface (such as the class name, properties, methods, or events) leads to the pages that reference the control must be updated.
  • Those pages must be re-compiled and needs deployment.
  • In addition, the next time a user views each page, the ASP.NET runtime will take a while to respond because the dynamic assembly for the page must be re-created.

Apart from the above two options, the other options is to use Master pages. A Master page is a file that contains the static layout of the file. It consists of the layout that is common throughout application (i.e. Application Level) or a folder level and dynamic parts will be customized by the pages that are derived from the Master page.

What is a Master Page?

One of the cool new things introduced in ASP.NET 2.0 is Master Pages. Master Pages give you the ability to define a master page layout and look that is used throughout a site to give a consistent look & feel to all pages. Any updates or changes to the look & feel of the site is done in only one place - the Master Page.

A master page is similar to an ordinary ASP.NET page except for the top @Master directive and the presence of one or more ContentPlaceHolder server controls. A ContentPlaceHolder

control defines a region in the master page that can be customized in a derived page. ContentPlaceHolder acts as container which holds controls/items defined in the derived pages.

<asp:contentplaceholder runat="server" ID="PageBody" />

In the derived pages, server control <asp:Content> is used to provide actual content to ContentPlaceHolders of Master Page. The link between placeholders and content is established through the Content place holder ID.

<asp:Content runat="server" contentplaceholderID="PageBody">
...
</asp:Content>

Note:

  1. In a master page, there can be multiple content place holders.
  2. Content page acts as bridge to fill the content in their master pages and it should only contains <asp:Content> server control. Everything (like different content) should be defined in that only.
  3. For a given Content place holder, default content can be defined in the master page itself. If it has not been overridden in Content page, the content defined in the master will be displayed.
  4. A placeholder can't be bound to more than one content region in a single content page. If you have multiple <asp:Content> server tags in a content page, each must point to a distinct placeholder in the master.
  5. A ContentPlaceHolder control can be used only in a master page. Content placeholders are not valid on regular ASP.NET pages. If such a control is found in an ordinary Web page, a parser error occurs.
  6. The MasterPage class, in turn, inherits UserControl. So, at the end of the day, a master page is treated as a special kind of ASP.NET user control.

Can we have a Master Page without Content Place Holders?

Yes.  We can have and ASP.Net runtime will compile the page, but the primary goal of the master page will not get satisfied.

Main Attributes of @Master Directive.

Attribute

Description

ClassName

Specifies the name for the class that will be created to render the master page. This value can be any valid class name but should not include a namespace.

By default, the class name for simple.master is ASP.simple_master.

Inherits

Specifies a code-behind class for the master page to inherit. This can be any class derived from MasterPage.

MasterPageFile

Specifies the name of the master page file that this master refers to. A master can refer to another master through the same mechanisms a page uses to attach to a master.

If this attribute is set, you will have nested masters.

Note:
The @Master directive doesn't override attributes set at the @Page directive level. Master Page can be built using one language (let say C#), derived page can be used another language like VB.Net.

What are the different ways to attach Pages to a Master?

Content page can be attached to Master in different ways

  1. Page Level: By using “MasterPageFile” attribute of @Page directive.
  2. Folder Level: Linking of the same Master Page to all the pages residing in the same folder. This can be achieved by defining in the web.config of the corresponding folder by using below tag.
  3. Application Level: Linking of the same Master Page to all the pages throughout the application. This can be achieved by defining in the web.config of the corresponding folder by using below tag.

<configuration>
<system.web>
<pages master="MyApp.master" />
</system.web>
</configuration>

There is an exception for Application Level binding, i.e. if we have application level binding, then all the pages in the application should map to one or more content place holders. In other words application level binding prevents from having (or later adding) a page to the site that is not configured as a content page. If there is any general asp.net page, that will throw exception. Make sure while using application level binding.

Can we have Master pages which are specific to environments?

Yes. We can have Master pages specific to device like browser (IE, netscape) specific. Master pages have the capability to identify the underlying browser and accordingly chooses master page.

Below is the syntax to achieve that feature

<%@ Page masterpagefile="Base.master" ie:masterpagefile="ieBase.master" netscape6to9:masterpagefile="nsBase.master" %>

When the page runs, the ASP.NET runtime automatically determines which browser or device the user is using and selects the corresponding master page. Here ieBase,master will be loaded when the site is accessed in the IE browser, nsBase.master will be loaded if it is Netscape browser, of the rest base.master will be utilised.

How the Master and Content Page will be processed internally?

Since Master and Content pages are interdependent, either of the page changes, dynamic assembly needs to be re-created.

When the user requests a page, first it checks whether master page exists or not. If there is any master page, then first master page will be compiled. If the folder consists of multiple master pages, all of them will be compiled at the same time. So when the user access any page for the first, page loading may take a little bit time for compiled, from the next time onwards, it won’t take much time, as master pages are already compiled and cached. When compared to User controls, this is an advantage for master pages as User Controls need compilation every time.

Can we have nested Master pages?

Yes. If a master page is nested from another master page, the derived master must consist of Content place holder along with Content and “MasterPageFile” of @Master directive will set to base Master page. There is no architectural limitation about the level of nesting a given master page.

Warning

Visual Studio 2008 is currently the only Microsoft tool capable of fully supporting nested master pages. If you're using older versions, you have to create an intermediate master page as a content page and then change the top directive to @Master, remove the Title attribute and, last but not least, change the base class of the code file to MasterPage.

Can we set Master page dynamically at runtime?

Yes. Set the MasterPageFile property only during the PreInit page event—that is, before the runtime begins working on the request (since the rendering of the page with the master page occurs prior to the Init event)

protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "simple2.master";
}

If you try to set the MasterPageFile property in Init or Load event handlers, an exception is raised.

Note:

The Master property represents the current instance of the master page object, is a read-only property, and can't be set programmatically. The Master property is set by the runtime after loading the content of the file referenced by the MasterPageFile property.

The above code needs to add in every page of the site. Instead, it is easy enough to add that code to a base page class that you can inherit from for you pages in the site.  In case if you do not already have the base page, you can use the following web.config settings to have a base class without having to modify the existing aspx pages.

<system.web>
    <!-- ... -->
    <pages pageBaseType="MyWeb.UI.MyPageBase" />
    <!-- ... -->
</system.web>

Can we access Master Page Controls from Content Page?

Yes.  We can access master page controls from the content pages.  Below are the 2 examples towards the same.

Example1:

TextBox tb = (TextBox)Master.FindControl("txtMaster");
tb.Text = "Something";

Example2:

ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder =
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
    mpTextBox = (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
    if(mpTextBox != null)
    {
        mpTextBox.Text = "TextBox found!";
    }
}

// Gets a reference to a Label control that is not in a
// ContentPlaceHolder control
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
    Label1.Text = "Master page label = " + mpLabel.Text;
}


  Last updated on Thursday, 13 March 2014
  Author: HariSantosh Kadiyala
3/5 stars (11 vote(s))

Register Login Ask Us Write to Us Help