Tuesday, May 12, 2009

Master Pages

Master Pages: The master page feature provides the ability to define common structure and interface elements for your site, such as page header, footer or navigation bar in a common location called master page

Adding master pages to solution:
A master page is similar to a ‘.aspx’ page except that it has its own unique extension, ‘.master’. Furthermore, a master page contains a new ‘Master’ directive, along with a ContentPlaceHolder control. we can keep any number of contentPlaceHolders in a single Master page.

Once you have your master page, you obviously want to create content pages using your master page as a template.

Your content page will be like any other .aspx page you have created in the past, except that it will reference the master page in the Page Directive and it will contain a Content Control and between this control will be all your content or at least the all the content for the corresponding ContentPlaceHolder contron in your master page.

<%@ Page Language=”C#” MasterPageFile=”~/Default.master” Title=”Untitled Page” %>

While adding the master page to the content bage, ‘MasterPageFile’ attribute will add with in the page directive.The use of this attribute indicates that this particular .aspx page inherits from another page.

The Location of the masterpage with in the application is specified as the value of the ‘MasterPageFile’ attribute.

After adding the Masterpage the contentpage (Default.aspx) neither contain

tag or any opening or closing HTML tags that would normally included in typical .aspx page.

Specifying which master page to use:

If you want to apply the master page template to only a specific subset of pages (such as pages contained within a specific folder of your application), you can use the element within the ‘Web.config’ file.

Working with masterpage Titles:

When you create content page in your application, by default all the content pages automatically use the title that is declared in the masterpage.

For instance, you have primarily been using a masterpage with the title “My Master page”. Every content page that created using the masterpage also uses the same “My Master page”.

You can avoid this by specifying the pages title using “Title” attribute in the page directive.

You can also work with the page titles programmatically in the content page.

Default.aspx:

Protected void page_LoadCompleted(object sender, EventArgs e)

{

Master.Page.Title=”This page Title”;

}

Working with controls and properties from the MasterPage:

The master page, when inherited by the content page, exposes a property called ‘Master’.

You can use this property to get at control values or custom properties that are contained in the Master page itself.

Default.master

Default.master Code behind:

Protected void page_Load(object sender, EventArgs e)

{

If(!page.IsPostBack)

{

Label1.Text=System.Guid.NewGuid().ToString();

}

}

Default.aspx

Protected void page_Load(object sender, EventArgs e)

{

Label1.Text=(Master.FindControl(“Label1”) as Label).Text;

}

Programatically Assigning the Master Page

You can assign the MasterPage to the content page through the use of the ‘Page.masterpagefile’ property. To accomplish this, use this property through the page_PreInIt event.

The Page_PreInit event is the earliest point in which you can access the page life cycle.

The Page_PreInit is an important event to make note of when you are working with MasterPages, as this the only point where you can affect both the master and content page before they are combined into a single instance.

Protected void page_PreInit(object sender, EventArgs e)

{

Page.masterpagefile=”~/MyMaster.master”;

}







No comments:

Post a Comment