Solved

How to put a conditional-dynamic stylesheet in header ASP.net C#

Posted on 2011-02-15
8
751 Views
Last Modified: 2013-11-19
I want to add this to my masterpage:

<!--[if IE]>
<link rel="stylesheet" href="css/clinicblue7.css" media="screen" type="text/css" />
<![endif]-->
<!-- tell iPhone not to shrink mobile website -->

But I want the set the "clinicblue7.css" part in the c# code.  I am currently adding other stylesheets dynamically using this method. How do I do a similar thing with the above referenced code.


System.Web.UI.HtmlControls.HtmlLink link = new HtmlLink();
        link.Attributes.Add("media", "screen and (min-device-width: 481px)");
        link.Href = "Css/" + value + ".css";
        link.Attributes.Add("type", "text/css");
        link.Attributes.Add("rel", "stylesheet");
        Page.Header.Controls.Add(link);

Open in new window

0
Comment
Question by:bcolladay
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34898957
Please see this URL:
http://www.aspnettutorials.com/tutorials/themes/usingcss-csharp.aspx

let me know if you are looking different.
0
 
LVL 4

Author Comment

by:bcolladay
ID: 34898976
Thanks, but I am just looking to use the code I referenced to see that the browser is Internet Explorer and to use a specified stylesheet that I will specify dynamically in the code behind.  This will have no end user interaction.
0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34898990
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34899062
You can't determine the client browser by using c# or vb.net.
You have to use client side scripts only.
0
 
LVL 9

Accepted Solution

by:
gdupadhyay earned 500 total points
ID: 34899119
Use this concept (Please let me know how it is):

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Browser.Browser == "IE")
            {
                System.Web.UI.HtmlControls.HtmlLink link = new HtmlLink();
                link.Attributes.Add("media", "screen and (min-device-width: 481px)");
                link.Href = "Css/" + value + ".css";
                link.Attributes.Add("type", "text/css");
                link.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(link);
            }
        }
    }
0
 
LVL 4

Author Comment

by:bcolladay
ID: 34899131
Yeah, I don't want to use c# to determine the browser.  I just want to dynamically specify what style sheet to use if that browser condition is met in the head section of html.  

this:
<!--[if IE]>
<link rel="stylesheet" href="css/clinicblue7.css" media="screen" type="text/css" />
<![endif]-->
<!-- tell iPhone not to shrink mobile website -->

is what I will be using but I need to change the name of the css based on other things happening.
0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34899170
"other things happening"... like?
0
 
LVL 4

Author Closing Comment

by:bcolladay
ID: 34899299
Thank you very much!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
Crystal Report 13.19  not working with ASP.Net framework 4.5.2 4 67
Add a Condition in ASP Repeater 6 34
exporting html table data 4 27
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question