Saturday, June 2, 2012

Using Master Pages in ASP.NET 4

Master Pages

Master pages contain a different page directive from normal pages.  Instead of @Page, masterpages have @Master.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

The Master page must have a contentplaceholder tag, where the pages deriving from this page will be inserted.

<asp:ContentPlaceHolder ID="MainContent" runat="server"/>

Content Pages

When creating a new page, you can derive from a selected Master Page by ticking 'Select master page'. After clicking 'Add', you will be prompted to select the masterpage from your website.

Adding a web page with master page
You can also do this by adding the following attribute in the @Page directive of the content page:

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>

This can also be done throughout the website by defining the masterpage in the pages element in the Web.config file. Any content pages that do not have the contentplaceholder will not apply the master page.

    <pages masterPageFile="~/Site.master" />

You can also change the page title from the content page by using the Title attribute in the Page directive.

The content page must contain a content control.  The markup written in this control will eventually be rendered in the contentplaceholder (with the same id) of the masterpage. You can have multiple content controls in a content page, as long as there are the same number of contentplaceholders in the masterpage

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        Put content here.

Note that the content place holder cannot be nested in other controls or tags or otherwise you will get the following error.  Same happens if there is no content placeholder in a content page.

Content controls have to be top-level controls in a content page or a nested master page that references a master page.

Note: Setting the EnableViewState property on the content page to true but setting the same property
to false in the master page, will result in having view state disabled because the setting on the
master page takes priority.

Getting controls from the Master Page

In order to get a control inside the master page from the content page you can use the FindControl method of the Master page property:

this.Master.FindControl("HeadLoginView") as LoginView;

Getting data from the Master Page

If you need a property from the master page, you will need to reference the master page in the content page by using the @MasterType directive

<%@ MasterType VirtualPath="~/Site.master" %>

Then from the content page, we can access the property that we need

lblUser.Text = this.Master.CurrentUser;

Changing Master Page dynamically

protected void Page_PreInit(object sender, EventArgs e)
this.MasterPageFile = String.Format("~/{0}.master", "Site2");

No comments:

Post a Comment