adsense

Wednesday, June 24, 2015

ASP.Net load Browser specific CSS

When developing Webpages you might have come across browser specific styling issues.
When dealing with IE related CSS issues conditional statements can be used as displayed below.


< !--[if IE]>
    <link rel="stylesheet" type="text/css" 
href="ie-specific.css" />
<![endif]-->
 
There will be situations where you will want to
target on different browser.
 E.g. Chrome, firefox
The code given below can be used to check the
browser type and load the 
CSS programatically.
 

 Dim Browser As HttpBrowserCapabilities = Request.Browser
        'Attach a CSS style sheet accordingly  
 Dim cs As ClientScriptManager = Nothing

If Browser.Type.StartsWith("IE") Then 'Target All IE
     cs = Page.ClientScript
     cs.RegisterClientScriptBlock(Me.GetType(), "CSSLink", "")
ElseIf Browser.Type = "Firefox" Then 'Actual version of the
                                 ' browser can be specified
     cs = Page.ClientScript
    cs.RegisterClientScriptBlock(Me.GetType(), "CSSLink", "")
 End If
 
Cheers
Samitha