Thursday, January 29, 2009

ASP.net MasterPage Advanced

1. Dynamically Setting MasterPage At Runtime

MasterPage can be set dynamically at runtime in Pre_Init method of Page.

protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.MasterPageFile = “<virtual path of .masterfile>";
    }

2. Strongly Type Access Of MasterPage And Its Properties.
 
MasterPage.aspx.cs
public partial class MainMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    { 
    } 
 
    public string PageTitle
    {
        get
        {
            return Page.Title;
        }
        set
        {
            Page.Title = value;
        }
    }
}

Now in Content Page need for access PageTitle Property.

In Content Page use @MasterType directive.

<%@ Page Language="C#" MasterPageFile="~/MainMaster.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MainMaster.master" %>

//Default2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        Master.PageTitle = "By use of MasterType";
    }
If @MasterType directive is not used than you can not access custom property of MasterPage . You are only limited to access MasterPage class property.
Note: When @MasterType directive used than it is not allowed to set page dynamically. It gives you type cast error as it try to convert object of one type to another type.

3. Access MasterPage Properties Using Reflection.   

By use of reflection you can also set property of master page.
For example access PageTitle Property (. It is not neccessary to specify MasterType directive on Page).

System.Reflection.PropertyInfo pinfo =  Page.Master.GetType().GetProperty("PageTitle");
        if (pinfo != null)
        {
pinfo.SetValue(Page.Master, "This is test reflection : Default", null);
        }

This approach is good if your requirement is set to masterpage at runtime.

4. By Use Of Common Interface For All MasterPage. 
    
Use this approach when decided that certain property must supported by MasterPage. In such case create one interface and that must be implemented by each of required masterpage.

// App_Code IMasterPageProperties.cs
public interface IMasterPageProperties
{
    string PageTitle
    {
        get;
        set;
    }
}


//SeconMaster.master.cs
public partial class SecondMaster : System.Web.UI.MasterPage , IMasterPageProperties
{
  //  Interface Implementation
  public string PageTitle
    {
        get
        {
            return Page.Title;
        }
        set
        {
            Page.Title = value;
        }
    }
}

// Defalt.aspx.cs (Page_Load event just for example)IMasterPageProperties property = Page.Master as IMasterPageProperties;
if (property != null)
        {
            property.PageTitle = "By use of Interface";
        }

Note : Try to cast Page.Master as Interface Type. This approach is good and cover both explicit and implicit implementation of interface.
5. By Use Of Abstract Base Class For MasterPage.

Create one abstract class . (Possibly in App_Code).

public abstract class BaseMasterPage : MasterPage
{
    public string PageTitle
    {
        get
        {
            return Page.Title;
        }
        set
        {
            Page.Title = value;
        }
    }
}

Now create new MasterPage ( I name it Third.master).  In normal case all masterpage inherits from MasterPage Class. Now ThirdMaster class should inherits from BaseMasterPage Class.

This is how Third.master.cs look like.

public partial class Third : BaseMasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
       this.PageTitle = "By use of abstarct base class";
    }
}

1 comment:

Rana said...

Nice tutorial. I have also written a basic tutorial for who are new in masterpage theme. They can read and get help here:

asp.net masterpage