I came across a position, where i have an application page developed for user requirement and the user desires the look and feel of this page to me similar to other pages in the context.
I tried so many alternatives but didn’t get prospered; I found the right to use the default.master or custom master page for application pages.
First thing is that if you need to use a masterpage from the site context, you want to have the similar content placeholders as are expected by the master page.
Master pages can be loaded dynamically. This can be done by assigning a master page file to the MasterPageFile property in the Page object. This property may only be assigned in the Page PreInit event, this is the first event executed page execution lifecycle.
Copy and paste this below code in your custom application page and deploy the assembly to GAC and see, it should work now.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
SPWeb TestWeb = SPControl.GetContextSite(Context).OpenWeb();
string strUrl = TestWeb.ServerRelativeUrl + “/_catalogs/masterpage/Test.master”;
this.MasterPageFile = strUrl;
}
Where “Test.master” has to be stored in master page gallery of the site.
Probably you may get a memory leak for SPWeb object , so you can use this simple code instead of the above
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
this.MasterPageFile = SPContext.Current.Web.MasterUrl;
}