Accessing a top-level master page from a web control

Discussion in 'ASP .Net' started by musosdev, Sep 1, 2008.

  1. musosdev

    musosdev Guest

    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
     
    musosdev, Sep 1, 2008
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:D...
    > 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
     
    Coskun SUNALI [MVP], Sep 1, 2008
    #2
    1. Advertising

  3. musosdev

    musosdev Guest

    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" <> wrote in message
    > news:D...
    > > 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

    >
    >
     
    musosdev, Sep 1, 2008
    #3
  4. musosdev

    musosdev Guest

    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" <> wrote in message
    > news:D...
    > > 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

    >
    >
     
    musosdev, Sep 1, 2008
    #4
  5. 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" <> wrote in message
    news:D...
    > 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" <> wrote in message
    >> news:D...
    >> > 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

    >>
    >>
     
    Coskun SUNALI [MVP], Sep 1, 2008
    #5
  6. musosdev

    musosdev Guest

    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" <> wrote in message
    > news:D...
    > > 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" <> wrote in message
    > >> news:D...
    > >> > 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
    > >>
    > >>

    >
     
    musosdev, Sep 2, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    609
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. Doug McCann
    Replies:
    1
    Views:
    1,154
    William Brogden
    Aug 5, 2004
  3. pabbu
    Replies:
    8
    Views:
    772
    Marc Boyer
    Nov 7, 2005
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,507
    Nathan Sokalski
    Mar 4, 2010
  5. John L.
    Replies:
    6
    Views:
    967
    Thomas 'PointedEars' Lahn
    Sep 4, 2011
Loading...

Share This Page