PC Review


Reply
Thread Tools Rate Thread

Accessing a top-level master page from a web control

 
 
musosdev
Guest
Posts: n/a
 
      1st Sep 2008
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
 
Reply With Quote
 
 
 
 
Coskun SUNALI [MVP]
Guest
Posts: n/a
 
      1st Sep 2008
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" <(E-Mail Removed)> wrote in message
news3467723-E03D-42E8-9901-(E-Mail Removed)...
> 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


 
Reply With Quote
 
musosdev
Guest
Posts: n/a
 
      1st Sep 2008
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" <(E-Mail Removed)> wrote in message
> news3467723-E03D-42E8-9901-(E-Mail Removed)...
> > 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

>
>

 
Reply With Quote
 
musosdev
Guest
Posts: n/a
 
      1st Sep 2008
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" <(E-Mail Removed)> wrote in message
> news3467723-E03D-42E8-9901-(E-Mail Removed)...
> > 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

>
>

 
Reply With Quote
 
Coskun SUNALI [MVP]
Guest
Posts: n/a
 
      1st Sep 2008
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" <(E-Mail Removed)> wrote in message
news1F5F06C-062B-4AFF-AB88-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news3467723-E03D-42E8-9901-(E-Mail Removed)...
>> > 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

>>
>>

 
Reply With Quote
 
musosdev
Guest
Posts: n/a
 
      2nd Sep 2008
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" <(E-Mail Removed)> wrote in message
> news1F5F06C-062B-4AFF-AB88-(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> >> news3467723-E03D-42E8-9901-(E-Mail Removed)...
> >> > 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
> >>
> >>

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing the master page's methods.. Ned White Microsoft ASP .NET 1 12th May 2008 02:19 AM
Accessing master page properties from server.transfer page? evantay Microsoft ASP .NET 4 30th May 2007 11:17 AM
accessing gridview from master page hazz Microsoft ASP .NET 0 25th Apr 2006 04:32 AM
Accessing method in master page from content page =?Utf-8?B?UmlrIEJyb29rcw==?= Microsoft C# .NET 4 23rd Dec 2005 12:23 PM
page-level vs control-level enableViewState =?Utf-8?B?Sm9l?= Microsoft ASP .NET 3 26th Oct 2005 08:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:47 PM.