Referencing properties of dynamically assigned master pages

M

Mark Rae

Hi,

It's easy enough to get a content page to refer to public properties on its
associated master page e.g.

public partial class master_secure : System.Web.UI.MasterPage
{
public Label MyLabel = new Label();
}

To refer to the label from a content placeholder, you would do the
following:

((master_secure)Master).MyLabel.Text = "Hello world";


It's also easy enough to change a content page's associated master page in
code e.g.

private void Page_PreInit(object sender, System.EventArgs e)
{
this.MasterPageFile = "~/master/" + Session["MasterPage"].ToString() +
".master";
}


Is there a way to "combine" the two i.e. change a content page's master page
and then be able to refer to the newly associated master page's public
properties? It's obviously easy enough to refer to the newly associated
master page's controls by using the FindControl() method, but I'm wracking
my brains about how to refer to its public properties.

Clearly, I could create a new base class which inherited MasterPage and add
the extra properites to that, but is there a simpler way?

Any assistance gratefully received.

Mark
 
K

Karl Seguin [MVP]

Use an interface

((ILabeledMaster)Master).MyLabel.Text = "Hello Word";

Karl
 
M

Mark Rae

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message
Hi Karl,

I found this site:
http://www.sadeveloper.net/Forums/ShowPost.aspx?PostID=56177 which was
particularly helpfiul, specifically Section III which talks about the
MasterType page directive. Following their example, I have got everything
working OK by creating a base master page class with the additional
properties and deriving the actual master pages off that.
Use an interface

((ILabeledMaster)Master).MyLabel.Text = "Hello Word";

I think that's pretty much the same thing.

Thanks again,

Mark
 

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,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top