Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Accessing a top-level master page from a web control

Reply
Thread Tools

Accessing a top-level master page from a web control

 
 
musosdev
Guest
Posts: n/a
 
      09-01-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
 
      09-01-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
news(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
 
      09-01-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
> news(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
 
      09-01-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
> news(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
 
      09-01-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
news(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
>> news(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
 
      09-02-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
> news(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
> >> news(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

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
How to Access Master Page Controls from page.aspx doesn't inherit from master SerpentKiss2010 ASP .Net 0 04-06-2011 02:46 PM
Setting the value of a Master page's control's property using a property of the Master page Nathan Sokalski ASP .Net 25 03-04-2010 03:42 AM
Can a master page be built from another master page Zeba ASP .Net 1 02-22-2007 10:55 AM
accessing the web user control's control from a web page and set a value from another web page Reny J Joseph Thuthikattu ASP .Net 1 12-30-2004 12:21 PM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM



Advertisments