MasterPages and applying CSS to content pages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mort Strom

    #1

    MasterPages and applying CSS to content pages

    Right now the header of my master page contains all of the CSS styles for
    all of the pages that might be loaded in my ContentPlaceHol der. The problem
    is that my <styletag is getting too large to manage. I have 300 lines of
    styles in my masterpage and I don't need all of this for every page --
    somehow this can't be a smart way of managing styles.

    How do I programmaticall y apply styles in my MasterPage based on the
    ContentPlaceHol der page that's being shown?

    Or another way to ask it - how do I apply styles to ContentPlaceHol der pages
    since these pages cannot contain a <headertag?

    Thanks for any direction,
    Mort

  • Mark Rae [MVP]

    #2
    Re: MasterPages and applying CSS to content pages

    "Mort Strom" <mail@rocksdrea m.comwrote in message
    news:89601CD3-9C87-493C-B954-35583BED8A07@mi crosoft.com...
    How do I programmaticall y apply styles in my MasterPage based on the
    ContentPlaceHol der page that's being shown?
    >
    Or another way to ask it - how do I apply styles to ContentPlaceHol der
    pages since these pages cannot contain a <headertag?
    Firstly, make sure you have runat="server" in the MasterPage's header tag
    e.g.

    <head runat="server">

    </head>

    Then, in the PageLoad event of the content page:

    Style objStyle = new Style();
    objStyle.ForeCo lor = System.Drawing. Color.Yellow;
    Header.StyleShe et.CreateStyleR ule(objStyle, null, "td");

    or

    HtmlLink objCSS = new HtmlLink();
    objCSS.Attribut es.Add("href", "~/css/DifferentStyle. css");
    objCSS.Attribut es.Add("rel", "stylesheet ");
    objCSS.Attribut es.Add("type", "text/css");
    Header.Controls .Add(objCSS);


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Mort Strom

      #3
      Re: MasterPages and applying CSS to content pages

      Well done! This is exactly the kind of solution I was looking for.
      I'm new to MasterPages. Thank you

      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
      news:#vSB9M5eIH A.4120@TK2MSFTN GP06.phx.gbl...
      "Mort Strom" <mail@rocksdrea m.comwrote in message
      news:89601CD3-9C87-493C-B954-35583BED8A07@mi crosoft.com...
      >
      >How do I programmaticall y apply styles in my MasterPage based on the
      >ContentPlaceHo lder page that's being shown?
      >>
      >Or another way to ask it - how do I apply styles to ContentPlaceHol der
      >pages since these pages cannot contain a <headertag?
      >
      Firstly, make sure you have runat="server" in the MasterPage's header tag
      e.g.
      >
      <head runat="server">
      >
      </head>
      >
      Then, in the PageLoad event of the content page:
      >
      Style objStyle = new Style();
      objStyle.ForeCo lor = System.Drawing. Color.Yellow;
      Header.StyleShe et.CreateStyleR ule(objStyle, null, "td");
      >
      or
      >
      HtmlLink objCSS = new HtmlLink();
      objCSS.Attribut es.Add("href", "~/css/DifferentStyle. css");
      objCSS.Attribut es.Add("rel", "stylesheet ");
      objCSS.Attribut es.Add("type", "text/css");
      Header.Controls .Add(objCSS);
      >
      >
      --
      Mark Rae
      ASP.NET MVP
      http://www.markrae.net

      Comment

      • Mort Strom

        #4
        Re: MasterPages and applying CSS to content pages

        If objPage1CSS is added to the Header for Content Page1.aspx, then
        objPage2CSS added for Content Page2.aspx, will the Header tag retain Page1 &
        2 CSS for subsequent pages (page3, page4, etc)?

        "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
        news:#vSB9M5eIH A.4120@TK2MSFTN GP06.phx.gbl...
        "Mort Strom" <mail@rocksdrea m.comwrote in message
        news:89601CD3-9C87-493C-B954-35583BED8A07@mi crosoft.com...
        >
        >How do I programmaticall y apply styles in my MasterPage based on the
        >ContentPlaceHo lder page that's being shown?
        >>
        >Or another way to ask it - how do I apply styles to ContentPlaceHol der
        >pages since these pages cannot contain a <headertag?
        >
        Firstly, make sure you have runat="server" in the MasterPage's header tag
        e.g.
        >
        <head runat="server">
        >
        </head>
        >
        Then, in the PageLoad event of the content page:
        >
        Style objStyle = new Style();
        objStyle.ForeCo lor = System.Drawing. Color.Yellow;
        Header.StyleShe et.CreateStyleR ule(objStyle, null, "td");
        >
        or
        >
        HtmlLink objCSS = new HtmlLink();
        objCSS.Attribut es.Add("href", "~/css/DifferentStyle. css");
        objCSS.Attribut es.Add("rel", "stylesheet ");
        objCSS.Attribut es.Add("type", "text/css");
        Header.Controls .Add(objCSS);
        >
        >
        --
        Mark Rae
        ASP.NET MVP
        http://www.markrae.net

        Comment

        • BobF

          #5
          Re: MasterPages and applying CSS to content pages

          "Mort Strom" <mail@rocksdrea m.comwrote in message
          news:89601CD3-9C87-493C-B954-35583BED8A07@mi crosoft.com...
          Right now the header of my master page contains all of the CSS styles for
          all of the pages that might be loaded in my ContentPlaceHol der. The
          problem is that my <styletag is getting too large to manage. I have 300
          lines of styles in my masterpage and I don't need all of this for every
          page -- somehow this can't be a smart way of managing styles.
          >
          How do I programmaticall y apply styles in my MasterPage based on the
          ContentPlaceHol der page that's being shown?
          >
          Or another way to ask it - how do I apply styles to ContentPlaceHol der
          pages since these pages cannot contain a <headertag?
          >
          Thanks for any direction,
          Mort
          Mort, I see you've been provided a rather elegant solution, but I'm curious
          why not use a separate style sheet?

          The site I'm working on now uses master pages and the style sheet linked to
          the master is being applied to the content pages without any extra effort.
          I'm asking because maybe there is something I can learn here.


          Comment

          • Mark Rae [MVP]

            #6
            Re: MasterPages and applying CSS to content pages

            "Mort Strom" <mail@rocksdrea m.comwrote in message
            news:35B464D7-0E29-4FBB-A6B1-B4AA52B3AC3D@mi crosoft.com...
            If objPage1CSS is added to the Header for Content Page1.aspx, then
            objPage2CSS added for Content Page2.aspx, will the Header tag retain Page1
            & 2 CSS for subsequent pages (page3, page4, etc)?
            The thing to realise about a MasterPage is that it's not a "page" in the way
            that an aspx page is a "page" - in fact, a MasterPage is little more than a
            UserControl... If the good folks who wrote ASP.NET had called them
            LayoutControls instead of MasterPages, then this would have been obvious...
            :-)

            You say that you are new to MasterPages, so maybe you're confusing them with
            framesets...?

            They are totally different...

            When a content page is requested, ASP.NET uses the MasterPage to construct
            the HTML which will eventually be streamed down to the client - it does this
            every time... The MasterPage does not remain on screen or in memory when you
            request a new content page...

            So, every time you request a content page which needs extra styles, they
            need to be added every time - that's why the code I gave you needs to go in
            the content pages' code-behind, not the MasterPage's code-behind...


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • Mark Rae [MVP]

              #7
              Re: MasterPages and applying CSS to content pages

              "BobF" <rNfOrSePeAzMe@ charter.netwrot e in message
              news:OtCgF26eIH A.2448@TK2MSFTN GP03.phx.gbl...
              Mort, I see you've been provided a rather elegant solution, but I'm
              curious why not use a separate style sheet?
              >
              The site I'm working on now uses master pages and the style sheet linked
              to the master is being applied to the content pages without any extra
              effort. I'm asking because maybe there is something I can learn here.
              The OP had a single style sheet which was being referenced directly in his
              MasterPage... However, this single stylesheet was becoming very large, and
              not all of the content pages required all of the styles in the sheet...

              Therefore, the solution I suggested allows him to break the stylesheet up
              into several more manageable stylesheets which can then be referenced as
              necessary by the content pages which need them...


              --
              Mark Rae
              ASP.NET MVP


              Comment

              • BobF

                #8
                Re: MasterPages and applying CSS to content pages


                "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                news:uWYSe86eIH A.5560@TK2MSFTN GP04.phx.gbl...
                "BobF" <rNfOrSePeAzMe@ charter.netwrot e in message
                news:OtCgF26eIH A.2448@TK2MSFTN GP03.phx.gbl...
                >
                >Mort, I see you've been provided a rather elegant solution, but I'm
                >curious why not use a separate style sheet?
                >>
                >The site I'm working on now uses master pages and the style sheet linked
                >to the master is being applied to the content pages without any extra
                >effort. I'm asking because maybe there is something I can learn here.
                >
                The OP had a single style sheet which was being referenced directly in his
                MasterPage... However, this single stylesheet was becoming very large, and
                not all of the content pages required all of the styles in the sheet...
                >
                Therefore, the solution I suggested allows him to break the stylesheet up
                into several more manageable stylesheets which can then be referenced as
                necessary by the content pages which need them...
                >
                Oh. I thought he had the style code embedded in the master page ...

                Thanks.


                Comment

                • Mort Strom

                  #9
                  Re: MasterPages and applying CSS to content pages

                  Indeed I was confusing MasterPages with framesets and your explanation has
                  cleared that up. Thanks again for helping bring MasterPages into clearer
                  focus.

                  On my way
                  Mort

                  "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                  news:eBgUC66eIH A.5560@TK2MSFTN GP04.phx.gbl...
                  "Mort Strom" <mail@rocksdrea m.comwrote in message
                  news:35B464D7-0E29-4FBB-A6B1-B4AA52B3AC3D@mi crosoft.com...
                  >
                  >If objPage1CSS is added to the Header for Content Page1.aspx, then
                  >objPage2CSS added for Content Page2.aspx, will the Header tag retain
                  >Page1 & 2 CSS for subsequent pages (page3, page4, etc)?
                  >
                  The thing to realise about a MasterPage is that it's not a "page" in the
                  way that an aspx page is a "page" - in fact, a MasterPage is little more
                  than a UserControl... If the good folks who wrote ASP.NET had called them
                  LayoutControls instead of MasterPages, then this would have been
                  obvious... :-)
                  >
                  You say that you are new to MasterPages, so maybe you're confusing them
                  with framesets...?
                  >
                  They are totally different...
                  >
                  When a content page is requested, ASP.NET uses the MasterPage to construct
                  the HTML which will eventually be streamed down to the client - it does
                  this every time... The MasterPage does not remain on screen or in memory
                  when you request a new content page...
                  >
                  So, every time you request a content page which needs extra styles, they
                  need to be added every time - that's why the code I gave you needs to go
                  in the content pages' code-behind, not the MasterPage's code-behind...
                  >
                  >
                  --
                  Mark Rae
                  ASP.NET MVP
                  http://www.markrae.net

                  Comment

                  Working...