Wednesday, August 02, 2006

Wednesday, June 07, 2006

I was trying to use two nested MasterPages. Let us say the top one is SuperMaster.master and I have some controls in it. The other MasterPage is, SubMaster.master, which is using SuperMaster.master as MasterPage and it also has some controls in it. Now I have used the following approach to set the SubMaster.master as the MasterPage for a Default.aspx.

public class Default : System.Web.UI.Page
{

protected override void OnPreInit(EventArgs e)

{
if (runtimeMasterPageFile != null)

{
this.MasterPageFile = "~SubMaster.master";
}

base.OnPreInit(e);
}
}



Let us say the ContentPlaceHolder in SuperMaster.master is "SuperContent".

If you want to get the refrence of the controls in the SuperMaster.master use the following syntax.

If suppose a TextBox named "TextBox1" is placed in SuperMaster.master then in Default.aspx you can get the reference of the control as follows...

TextBox txt = (TextBox)this.Master.Master.FindControl("TextBox1");

If you have placed a DropDownList named "DropDownList1" in SubMaster.master and to get the reference of the same in Default.aspx you are wrong if you think that the following will work.

DrownDownList ddl = (DrownDownList)this.Master.FindControl("DropDownList1");

The above statement will give runtime error.

The solution for this is you need to get the control reference form the ContentPlaceHolder which is placed in SuperMaster.master as follows...

//To get the ContentPlaceHolder reference placed in SuperMaster.matser
ContentPlaceHolder cntPlh = (ContentPlaceHolder)this.Master.Master.FindControl("SuperContent");

//To get the DropDownList Reference from the SubMaster.master in Default.aspx
DrownDownList ddl = (DrownDownList)cntPlh.FindControl("DropDownList1");

This problem i have faced for two days and finally i got the solution....
Thats all you can enjoy the coding using Nested MasterPages....

PanduKovur