I
iturner100
Hi,
I've been struggling with this one for a couple of hours without much
joy.
Basically, I've got a set of nested masterpages (3 as it happens). I'm
dynamically generating a new page in code and want to be able to
populate the ContentPlaceholders of the inner-most masterpage at
run-time.
So, assuming the masterpages are named Corporate.Master,
Division.Master, Dept.Master what I want to be able to do is
dynamically load some content into the placeholders of the Dept.Master.
Now, I have control over Dept.Master so I know what ID's I've given to
the placeholders on it. I don't, however, know what the placeholder
ID's are for the Division.Master and Dept.Master - at least not at
run-time. The actual html that i squirt into the placeholders on the
Dept.Master is held in SQL. Each piece of html has an ID associated
with it which is the content placeholder in which to be squirted...
The purpose of this is to allow the Corporate and Division masterpages
to be hacked around at someone else's discretion. And because i'm
dynamically creating the aspx pages I don't need to worry about that.
All I need to do is know how to get a reference to the content
placeholder of my Dept.Master from code...
Now, this is the bit that is stumping me. FindControl doesn't seem to
work on the nested masterpages. It appears that I have to call
FindControl on the outer-most masterpage (in this case
Corporate.Master) and use the qualified ID - something like
page.Master.Master.Master.FindControl("CorporateBodyPh
ivisionBodyPh
eptBodyPh").
All i store in SQL is the DeptBodyPh id and the actual html i want to
use.
Unfortunately, I can't see anyway of determing the nesting path of my
placeholders to be able to construct the ID correctly.
Can anyone shed any light? Apologies for this being a bit of a ramble.
That can be put down in part to the lateness of the hour, but mostly to
the pub i've just returned from...
Cheers
Ian
And for a final bit of ramble, a code snippet:
void BuildPage()
{
// Dynamically create a page
Page page = new Page();
// Associate it with a master page
page.MasterPageFile = "~/Dept.Master" // retrieved from SQL
page.AppRelativeVirtualPath = "~/";
// Get the placeholder content from SQL
Dictionary<string, string> placeholders = TalkToSQL();
// Populate placeholders
foreach (string placeholderId in placeholders.Keys)
{
// Get the content placeholder - the bit that is causing grief
ContentPlaceholder ph = page.Master.FindControl(placeholderId);
// Fill it with html
LiteralControl lit = new
LiteralControl(placeholders[placeholderId]);
// And add to the ph
ph.Controls.Add(lit);
}
// Should be done now
page.ProcessRequest();
}
I've been struggling with this one for a couple of hours without much
joy.
Basically, I've got a set of nested masterpages (3 as it happens). I'm
dynamically generating a new page in code and want to be able to
populate the ContentPlaceholders of the inner-most masterpage at
run-time.
So, assuming the masterpages are named Corporate.Master,
Division.Master, Dept.Master what I want to be able to do is
dynamically load some content into the placeholders of the Dept.Master.
Now, I have control over Dept.Master so I know what ID's I've given to
the placeholders on it. I don't, however, know what the placeholder
ID's are for the Division.Master and Dept.Master - at least not at
run-time. The actual html that i squirt into the placeholders on the
Dept.Master is held in SQL. Each piece of html has an ID associated
with it which is the content placeholder in which to be squirted...
The purpose of this is to allow the Corporate and Division masterpages
to be hacked around at someone else's discretion. And because i'm
dynamically creating the aspx pages I don't need to worry about that.
All I need to do is know how to get a reference to the content
placeholder of my Dept.Master from code...
Now, this is the bit that is stumping me. FindControl doesn't seem to
work on the nested masterpages. It appears that I have to call
FindControl on the outer-most masterpage (in this case
Corporate.Master) and use the qualified ID - something like
page.Master.Master.Master.FindControl("CorporateBodyPh
All i store in SQL is the DeptBodyPh id and the actual html i want to
use.
Unfortunately, I can't see anyway of determing the nesting path of my
placeholders to be able to construct the ID correctly.
Can anyone shed any light? Apologies for this being a bit of a ramble.
That can be put down in part to the lateness of the hour, but mostly to
the pub i've just returned from...
Cheers
Ian
And for a final bit of ramble, a code snippet:
void BuildPage()
{
// Dynamically create a page
Page page = new Page();
// Associate it with a master page
page.MasterPageFile = "~/Dept.Master" // retrieved from SQL
page.AppRelativeVirtualPath = "~/";
// Get the placeholder content from SQL
Dictionary<string, string> placeholders = TalkToSQL();
// Populate placeholders
foreach (string placeholderId in placeholders.Keys)
{
// Get the content placeholder - the bit that is causing grief
ContentPlaceholder ph = page.Master.FindControl(placeholderId);
// Fill it with html
LiteralControl lit = new
LiteralControl(placeholders[placeholderId]);
// And add to the ph
ph.Controls.Add(lit);
}
// Should be done now
page.ProcessRequest();
}