Accessing a top-level master page from a web control

  • Thread starter Thread starter musosdev
  • Start date Start date
M

musosdev

Hi guys

I'm trying to make my code as streamlined as possible, and add CSS file
references dynamically when they are required, for example, if a page
contains a webcontrol, then the related CSS file is added by the webcontrol.

This prevents me having to remember to add the CSS file to the page if I use
a certain webcontrol.

I have a MasterPage with an array of StyleSheets, and a public function for
adding new stylesheets. That works great from either a content page, or the
content page of a nested master.

However, how can I do it from webcontrol? The nesting is something like...

MyMaster.master - contains public AddStyleSheet() function
- AboutMySite.master
- About_Home.aspx
- MyWebControl.aspx - this needs to be able to add its stylesheet using
AddStyleSheet() from the top-level master.

Is what I'm trying to do possible, and if so how can I this?!

Thanks,


Dan
 
Hi Dan,

Yes, it is possible. You master page has a type, depending on what name you
gave to it. I will assume that it is "MyMaster".

So, simply:

((MyMaster)Page.Master).AddStyleSheet(...);


--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
 
Hi again,

Although you're code is right, in my situation I'm getting an error....

Unable to cast object of type 'ASP._mp_about_master' to type 'musosmaster'.

This is coming from the following line of code on my web control...

((musosmaster)Page.Master).StyleSheets.Add(new
StyleSheet("widget_fromtheblog.css", "screen"));

....where the webcontrol is on master page (_mp_about), which is nested
inside another master page (musosmaster), which is where the
StyleSheets.Add() functionality is.

Can you explain how to modify the code so it's looking at the parent
masterpage, rather than the masterpage that holds the control!?

Thanks,


Dan
 
Hi Dan,

"MasterPage" class has also a property called "Master". So, in your case,
you may use "Page.Master.Master".

Description of Master property within MasterPage class from MSDN: Gets the
parent master page of the current master in nested master pages scenarios.

--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
 
Hi

That worked great, thank you! I now have a peice of code in each of the web
controls that can find the MasterPage and insert the stylesheet.

One thing I did notice, I tried to put a function in a static class to do
it, but it wouldn't let me, saying there was no "this" inside a static
function. Here's the code I was trying...

public static void InsertStyleSheet(StyleSheet css)
{
try { ((musosmaster)Page.Master).StyleSheets.Add(css); }
catch { ((musosmaster)Page.Master.Master).StyleSheets.Add(css); }
}

Can anyone point me in the direction how I might be able to get this
function working globally, rather than having to add it to each WebControls'
cs file?! I thought about putting it in my master page, but then I'd have the
same problem finding it from the webcontrol I think?!


Dan
 
Back
Top