By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

Nested Masterpages and naming

P: n/a
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("CorporateBo dyPh: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();
}

Feb 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"it********@gmail.com" wrote:
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("CorporateBo dyPh: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();
}

Feb 8 '06 #2

P: n/a
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...

Feb 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.