How to dynamically change the style sheet based on browser type

Discussion in 'ASP .Net' started by Rob Roberts, Jan 20, 2006.

  1. Rob Roberts

    Rob Roberts Guest

    In a VS2005 ASP.NET project, I'm trying to find a way to change which css
    file is linked in based on the browser type. I'd like to use one css file
    for IE browsers, and a different one for all other browsers. I'm using a
    MasterPage. I added a Literal control to the Head section of the
    MasterPage, and then in the Page_Load event I set its Text property to set
    up the desired link, like this:

    StyleSheetLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\"
    href=\"/inc/content_ie.css\">";

    This works, but the VS2005 IDE reports it as an error, with message
    "Unrecognized tag prefix or device filter 'asp'." Plus I can't help
    thinking that there must be a better way.

    I also tried setting the Page.StyleSheetTheme property in the Page_Load
    event, but this gives a System.InvalidOperation exception when the page
    loads. The documentation for Page.StyleSheetTheme says that it must be set
    before the page's Init event completes, but I can't figure out how to access
    that event in the MasterPage.

    Any ideas on the best way to accomplish setting the style sheet dynamically?

    Thanks in advance,
    --Rob Roberts
     
    Rob Roberts, Jan 20, 2006
    #1
    1. Advertising

  2. The <link>tag in ASP.NET 2.0 can runat=server, e.g. you can place on the
    master page:
    <link runat="server" id="StyleLink1" type="text/css"/>

    You can access it directly on the master page like this:
    StyleLink1.Href ="MyStyleSheet";

    Or you can access it in the content page like this:

    //assuming that you have
    //<%@ masterType VirtualPath="~/MasterPage.master" %> in your page markup
    HtmlLink lnk = (HtmlLink)Master.FindControl("StyleLink1");
    if (lnk != null) lnk.Href="MyStyleSheet";

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Rob Roberts" wrote:

    > In a VS2005 ASP.NET project, I'm trying to find a way to change which css
    > file is linked in based on the browser type. I'd like to use one css file
    > for IE browsers, and a different one for all other browsers. I'm using a
    > MasterPage. I added a Literal control to the Head section of the
    > MasterPage, and then in the Page_Load event I set its Text property to set
    > up the desired link, like this:
    >
    > StyleSheetLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\"
    > href=\"/inc/content_ie.css\">";
    >
    > This works, but the VS2005 IDE reports it as an error, with message
    > "Unrecognized tag prefix or device filter 'asp'." Plus I can't help
    > thinking that there must be a better way.
    >
    > I also tried setting the Page.StyleSheetTheme property in the Page_Load
    > event, but this gives a System.InvalidOperation exception when the page
    > loads. The documentation for Page.StyleSheetTheme says that it must be set
    > before the page's Init event completes, but I can't figure out how to access
    > that event in the MasterPage.
    >
    > Any ideas on the best way to accomplish setting the style sheet dynamically?
    >
    > Thanks in advance,
    > --Rob Roberts
    >
    >
    >
    >
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 21, 2006
    #2
    1. Advertising

  3. Rob Roberts

    Rob Roberts Guest

    Thank you very much! That worked perfectly.

    --Rob Roberts

    "Phillip Williams" <> wrote in message
    news:...
    > The <link>tag in ASP.NET 2.0 can runat=server, e.g. you can place on the
    > master page:
    > <link runat="server" id="StyleLink1" type="text/css"/>
    >
    > You can access it directly on the master page like this:
    > StyleLink1.Href ="MyStyleSheet";
    >
    > Or you can access it in the content page like this:
    >
    > //assuming that you have
    > //<%@ masterType VirtualPath="~/MasterPage.master" %> in your page markup
    > HtmlLink lnk = (HtmlLink)Master.FindControl("StyleLink1");
    > if (lnk != null) lnk.Href="MyStyleSheet";
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Rob Roberts" wrote:
    >
    >> In a VS2005 ASP.NET project, I'm trying to find a way to change which css
    >> file is linked in based on the browser type. I'd like to use one css
    >> file
    >> for IE browsers, and a different one for all other browsers. I'm using a
    >> MasterPage. I added a Literal control to the Head section of the
    >> MasterPage, and then in the Page_Load event I set its Text property to
    >> set
    >> up the desired link, like this:
    >>
    >> StyleSheetLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\"
    >> href=\"/inc/content_ie.css\">";
    >>
    >> This works, but the VS2005 IDE reports it as an error, with message
    >> "Unrecognized tag prefix or device filter 'asp'." Plus I can't help
    >> thinking that there must be a better way.
    >>
    >> I also tried setting the Page.StyleSheetTheme property in the Page_Load
    >> event, but this gives a System.InvalidOperation exception when the page
    >> loads. The documentation for Page.StyleSheetTheme says that it must be
    >> set
    >> before the page's Init event completes, but I can't figure out how to
    >> access
    >> that event in the MasterPage.
    >>
    >> Any ideas on the best way to accomplish setting the style sheet
    >> dynamically?
    >>
    >> Thanks in advance,
    >> --Rob Roberts
    >>
    >>
    >>
    >>
    >>
    >>
     
    Rob Roberts, Jan 21, 2006
    #3
    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. Simon
    Replies:
    2
    Views:
    2,155
  2. Replies:
    0
    Views:
    569
  3. _Who
    Replies:
    0
    Views:
    324
  4. Ken Varn
    Replies:
    0
    Views:
    538
    Ken Varn
    Apr 26, 2004
  5. Hank Flowers

    Problem dynamically loading style sheet

    Hank Flowers, Apr 10, 2004, in forum: Javascript
    Replies:
    0
    Views:
    170
    Hank Flowers
    Apr 10, 2004
Loading...

Share This Page