tags:

views:

40

answers:

4

Hi, I want to implement different CSS style sheet using javascript or code behind on aspx page so that for different web browser my page look better. Can anyone have some solution about this problem? I try a lot to implement that but failed.

A: 

I'd go with the server side option (aspx in your case).

  1. check the 'user_agent' request header to determine the user's browser type
  2. logically include a different css file based on this variable
pstanton
+2  A: 

Generally you don't want to go down the route of dynamically generating CSS with Javascript. The best approach to CSS is to:

  1. Use a reset CSS;
  2. Declare a DOCTYPE on every page; and
  3. If necessary, include IE-specific additions (because, let's face it, it's always IE that causes the problems).
cletus
A: 

HtmlLink styleSheet = new HtmlLink(); styleSheet.Attributes.Add("rel","stylesheet"); styleSheet.Attributes.Add("type","text/css"); styleSheet.Attributes.Add("href",ResolveClientUrl("MyStyleSheet.css"));

this.Page.Header.Controls.Add(styleSheet);

Check this out.

You can even set the style by adding a literal to your head tag and add the css style as text to this literal.

Happy coding.

Ravia
+1  A: 

To add to Ravia: You can use Request.Browser to get browser versions:

HttpBrowserCapabilities bc = Request.Browser;
if (bc.Browser == "IE" && bc.Version == "6.0")
{
    HtmlLink link = new HtmlLink();
    link.Href = ResolveClientUrl("~/CSSFile.css");
    link.Attributes.Add("rel", "stylesheet");
    link.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(link);
}
Kumu