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

Accessing a top-level master page from a web control

P: n/a
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
Sep 1 '08 #1
Share this Question
Share on Google+
5 Replies


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

"musosdev" <mu*******@community.nospamwrote in message
news:D3**********************************@microsof t.com...
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
Sep 1 '08 #2

P: n/a
That works, thanks!

"Coskun SUNALI [MVP]" wrote:
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

"musosdev" <mu*******@community.nospamwrote in message
news:D3**********************************@microsof t.com...
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

Sep 1 '08 #3

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


"Coskun SUNALI [MVP]" wrote:
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

"musosdev" <mu*******@community.nospamwrote in message
news:D3**********************************@microsof t.com...
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

Sep 1 '08 #4

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

"musosdev" <mu*******@community.nospamwrote in message
news:D1**********************************@microsof t.com...
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


"Coskun SUNALI [MVP]" wrote:
>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

"musosdev" <mu*******@community.nospamwrote in message
news:D3**********************************@microso ft.com...
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

Sep 1 '08 #5

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


"Coskun SUNALI [MVP]" wrote:
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

"musosdev" <mu*******@community.nospamwrote in message
news:D1**********************************@microsof t.com...
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


"Coskun SUNALI [MVP]" wrote:
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

"musosdev" <mu*******@community.nospamwrote in message
news:D3**********************************@microsof t.com...
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

Sep 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.