Nested Masterpages and naming

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:DivisionBodyPh:DeptBodyPh").
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();
}
 
G

Guest

I had a similar problems with nested masterpages findcontrol due to the weird
naming that maserpages causes. Eventually was able to get at things using
findcontrol on the Form directly.

Form1.FindControl("controlname")

mike


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:DivisionBodyPh:DeptBodyPh").
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

iturner100

Oh, that sounded so promising but, alas, it didn't work either. Not to
worry, I think I'm going to rejig the way I'm structuring my project so
it doesn't use a third-tier of masterpage, instead uses templated aspx
pages that can render the content through normal
System.Web.UI.Placeholder controls. Hopefully that will get me
further...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top