是否始终的Request.Browser工作如预期? [英] Does Request.Browser work always as expected?
问题描述
我有下面这段code的加入正确的CSS文件中的头根据浏览器,
I have the following piece of code to add the right CSS file in the "head" depending on browser,
string browserName = Request.Browser.Browser;
string browserVersion = Request.Browser.Version;
Control Head = Page.Master.FindControl("stuHead");
if (Head != null)
{
if (browserName == "IE")
{
if (browserVersion == "6.0")
{
Head.Controls.Add(new LiteralControl("<link rel='stylesheet' rev='stylesheet' href='Home-IE6.css' type='text/css' media='all' />"));
}
else
{
Head.Controls.Add(new LiteralControl("<link rel='stylesheet' rev='stylesheet' href='Home-IE.css' type='text/css' media='all' />"));
}
}
else
{
Head.Controls.Add(new LiteralControl("<link rel='stylesheet' rev='stylesheet' href='Home.css' type='text/css' media='all' />"));
}
}
else
{
Response.Write("<link rel='stylesheet' rev='stylesheet' href='Home.css' type='text/css' media='all' />");
}
当我在IE8打开我的网页,有时我看到Home.css,其实我应该看到的Home-IE.css。我已确保头不为空。不知道是否有人已经经历过这样的事情。任何意见AP preciated。
When I open my page in IE8, sometimes I see the Home.css, actually I should be seeing the Home-IE.css. I have ensured that the Head is not null. Not sure if anyone has experienced such a thing. Any comments appreciated.
推荐答案
我不知道你为什么不HTML中的部分做...有大量的网站解释的 CSS招。
I wonder why don't you do it in HTML part... there are plenty of websites explain this css trick.
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="all-ie.css" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="common.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="all-ie.css" />
<![endif]-->
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="ie-6.0.css" />
<![endif]-->
<!--[if lt IE 6]>
<link rel="stylesheet" type="text/css" href="ie-5.0+5.5.css" />
<![endif]-->
只是想指出的东西在你的code,尝试学习如何重构,如何重用code。
just to point out something on your code, try to learn how to Refactor and how to Reuse code.
string browserName = Request.Browser.Browser;
string browserVersion = Request.Browser.Version;
if (Head != null)
{
if (browserName == "IE")
{
if (browserVersion == "6.0")
AddCSS("Home-IE6.css", false);
else
AddCSS("Home-IE.css", false);
}
else
AddCSS("Home.css", false);
}
else
AddCSS("Home.css", true);
...
void AddCss(string cssFile, bool write) {
Control Head = Page.Master.FindControl("stuHead");
String css = String.Format("<link rel='stylesheet' rev='stylesheet' href='{0}' type='text/css' media='all' />", cssFile);
if(write)
Response.Write(css);
else
Head.Controls.Add(new LiteralControl(css));
}
这篇关于是否始终的Request.Browser工作如预期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!