I use master page to define the page layout of default.aspx. In the master page I have included ContentPlaceHolder cph. I want to access cph from default.aspx and load the ascx file into cph. How can I do this? I tried the code below in page load event, but it does not work.
string controlPath = "~/Usercontrols/webusercontrol.ascx";
UserControl uc = LoadControl(controlPath) as UserControl;
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("cphMain");
if ((cph != null) && (uc != null))
{
cph.Controls.Add(uc);
}
Make sure your Default.aspx has this on the page tag: MasterPageFile=”~/MasterPages/Master1.master”
Once you are sure of that, you can use this tag in your Default.aspx
You can just put your user in here, or you can add it dynamically. I would recommend against dynamic loading unless it is necessary (dynamic controls adds an extra layer of complexity)