Dynamically Adding Content to Nested Master Pages

E

Elroyskimms

I'm new to Master Pages so if this is an easy fix, I apologize in
advance.
I have 4 files in a website: default.aspx, Root.Master, M2.Master,
UC1.ascx

default.aspx is accessed via a web browser. Its Master Page is set to
Root.Master:

<%@ Page Language="vb" MasterPageFile="~/Root.Master" %>
<script runat="server">
</script>

<asp:Content ID="defaultContentArea" runat="server"
ContentPlaceHolderID="cphDefault">
<div style="background-color: Blue; color: White; font-size:
larger">
<table width="100%">
<tr valign="middle">
<td>
Menu Item</td>
<td>
Menu Item</td>
</tr>
</table>
</div>
</asp:Content>
___________________________________________
Root.Master has 2 ContentPlaceHolders. The top holder (cphDefault) is
where default.aspx will be loaded (this appears to be working fine.
The bottom ContentPlaceHolder is cphRoot and M2.Master is set to have
cphRoot as its Content Holder. I add M2.Master to cphRoot at runtime:

<%@ Master Language="VB" %>

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
cphRoot.Controls.Add(Page.LoadControl("~/M2.Master"))
End Sub
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="5" cellspacing="0" style="border:
solid medium red;" width="100%">
<tr valign="top">
<td style="border: solid thick blue;">
<asp:ContentPlaceHolder ID="cphDefault"
runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
<tr valign="top">
<td style="border: solid thick green;">
<asp:ContentPlaceHolder ID="cphRoot"
runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
_______________________________________________
M2master has one ContentPlaceHolder. I add UC1.ascx to the
LeftContentArea at runtime. Please note that I have tried this code on
the Init and Load events, but the outcome is the same:

<%@ Master Language="VB" MasterPageFile="~/Root.Master" %>

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
LeftContentArea.Controls.Add(Page.LoadControl("~/UC1.ascx"))
End Sub
</script>

<asp:Content ID="M2Content" runat="server"
ContentPlaceHolderID="cphRoot">
M2
<table width="100%" cellpadding="0" border="5">
<tr valign="top">
<td width="50%">
<asp:ContentPlaceHolder ID="LeftContentArea"
runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
</asp:Content>
__________________________________________
The code runs fine until M2.Master tried to add UC1.ascx to the
LeftContentArea. LeftContentArea is always nothing so the load fails.
I've tried every possible combination of Init and Load events for both
Master pages. I've also tried creating a new instance of a
ContentPlaceHolder and assigning that to Me.LeftContentArea before
loading the UC1.ascx control. Although this runs fine, the user
control is not visible when the page loads into the browser.

What I am trying to accomplish is to populate the cphRoot
ContentPlaceHolder (in Root.Master) with a different Master page which
will then display a Web User Control. The idea is to be able to load
various layout Master pages at runtime and then fill their dynamically
generated ContentPlaceHolders with Web User Controls. Please help!

Thanks,

-E
 
E

Elroyskimms

Page.Controls.Add(Page.LoadControl("WebUserControl1.ascx"))


Thanks for the response. I am away from the office for the day so I
won't be able to try this just yet, but I was curious... if M2.Master
were to have more than one ContentPlaceHolder, how would I choose
which ContentPlaceHolder to display the Web User Control?

-E
 
E

Elroyskimms

Thanks for the response. I am away from the office for the day so I
won't be able to try this just yet, but I was curious... if M2.Master
were to have more than one ContentPlaceHolder, how would I choose
which ContentPlaceHolder to display the Web User Control?

-E

I think I may have figured it out. The problem wasn't how I was
loading the controls to the page, it was in the Master Page markup.
Each asp:Content control and asp:ContentPlaceHolder control was mapped
to its "parent" Master Page in the HTML markup. That works great for
when you are loading a page which is calling the bottom of that nested
stack of Master Pages (traditional nested Master Page layouts). The
problem is that I was starting at the top of the nested Master Pages
and working my way in the opposite direction. Once I removed all of
the attributes in the asp:Content tag, everything worked exactly as I
expected.

Of course now my HTML doesn't pass validation :-( I don't mind as long
as it works, but I imagine the red squigly line is there for a reason.
I may regret relying on improper markup. If I run into any issues, I
will post them here so that no one else reads this post and uses
broken HTML tags.

-E
 

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

Top