Solved

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

Posted on 2011-02-15
8
743 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now