IE 7中PostBack上的ASP.NET CSS无效

时间:2021-04-09 03:18:06

When My ASP.NET page does a postback in IE7, all the CSS is lost. I am not using Themes and are including the CSS from the Masterpage in code behind.

当我的ASP.NET页面在IE7中进行回发时,所有的CSS都会丢失。我没有使用主题,并且在代码后面的主页中包含了CSS。

    protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    AddStylesheetInclude("/static/css/global.css");
    AddStylesheetInclude("/static/css/sifr.css");
}

And my code to actually do the adding:

我的代码实际上是添加:

public virtual void AddStylesheetInclude(String url)
{
    HtmlLink link = new HtmlLink();
    link.Attributes["type"] = "text/css";
    link.Attributes["href"] = url;
    link.Attributes["rel"] = "stylesheet";
    Page.Header.Controls.Add(link);
}

It works perfectly fine in Firefox, just IE. To add more context, in the CSS file included, it uses @import to include the rest.

它在Firefox中运行得非常好,只是IE。要添加更多上下文,在包含的CSS文件中,它使用@import来包含其余内容。

[Edit] It works if I take all the @import rules and have it included in the actual HTML.

[编辑]如果我采用所有@import规则并将其包含在实际HTML中,它就可以工作。

4 个解决方案

#1


After doing more research on your problem I found out the reason is doesn't work with the link is because (http://www.broken-links.com/2007/02/15/ie7-and-import-media-types/ )IE7 isn't compatible with @import. Which is kind of a good thing, because IE7 fixed the '!important' and '* html' debugging hacks, so without the @import bug there wouldn't be an easy way to debug in IE7. But I am sure they will fix it next time around (5 years or so?) and at that point debugging in IE* will be h*ll.

在对你的问题做了更多研究后,我发现原因是因为链接不起作用是因为(http://www.broken-links.com/2007/02/15/ie7-and-import-media-types /)IE7与@import不兼容。这是一件好事,因为IE7修复了'!important'和'* html'调试黑客,所以没有@import错误就没有一种简单的方法可以在IE7中进行调试。但我相信他们下次会修复它(大约5年左右?),此时IE *中的调试将是h * ll。

So I guess this is the only way for now to try adding the css script on the client side so it will always remain rendered on the page. after you build with the link included of the css file your IntelliSense should pick up your classes when you try to add classes to controls in your source file..if that does not happen something may be wrong with your link to the file, so instead incorporate your css code in your source as so:

所以我想这是现​​在尝试在客户端添加css脚本的唯一方法,因此它将始终保持在页面上呈现。在使用css文件中包含的链接构建之后,当您尝试将类添加到源文件中的控件时,IntelliSense应该选择类。如果没有发生,则链接到文件可能会出错,因此将您的css代码合并到源代码中:

<style type="text/css"> 

.highlight{               
background-color: #C0DDE0;     
}
</style>

and you should be able to something like this

你应该能够做到这样的事情

     <table id="table1" class="highlight"/>

#2


If the css still doesn't work even after you have placed a link-tag directly in the head, i'd say that there isn't anything wrong with the way you include the css, but rather the css-files themselves.

如果即使你已经直接在头部放置了一个链接标签,css仍然不起作用,我会说你包含css的方式没有任何问题,而是css文件本身。

Try including them all without the @import directly in the head. That way you can rule out any issues with the import.

尝试将它们全部包含在内,而不将@import直接放在头部。这样您就可以排除导入的任何问题。

#3


Have you verified that the tag is still on the page after a postback? I seem to remember something about difficulties manipulating the HEAD tag reliably.

您是否在回发后验证了标签是否仍在页面上?我似乎记得有关可靠地操纵HEAD标签的困难。

#4


Hmm, is there any reason why you reference your CSS files like this?

嗯,你有没有理由像这样引用你的CSS文件?

#1


After doing more research on your problem I found out the reason is doesn't work with the link is because (http://www.broken-links.com/2007/02/15/ie7-and-import-media-types/ )IE7 isn't compatible with @import. Which is kind of a good thing, because IE7 fixed the '!important' and '* html' debugging hacks, so without the @import bug there wouldn't be an easy way to debug in IE7. But I am sure they will fix it next time around (5 years or so?) and at that point debugging in IE* will be h*ll.

在对你的问题做了更多研究后,我发现原因是因为链接不起作用是因为(http://www.broken-links.com/2007/02/15/ie7-and-import-media-types /)IE7与@import不兼容。这是一件好事,因为IE7修复了'!important'和'* html'调试黑客,所以没有@import错误就没有一种简单的方法可以在IE7中进行调试。但我相信他们下次会修复它(大约5年左右?),此时IE *中的调试将是h * ll。

So I guess this is the only way for now to try adding the css script on the client side so it will always remain rendered on the page. after you build with the link included of the css file your IntelliSense should pick up your classes when you try to add classes to controls in your source file..if that does not happen something may be wrong with your link to the file, so instead incorporate your css code in your source as so:

所以我想这是现​​在尝试在客户端添加css脚本的唯一方法,因此它将始终保持在页面上呈现。在使用css文件中包含的链接构建之后,当您尝试将类添加到源文件中的控件时,IntelliSense应该选择类。如果没有发生,则链接到文件可能会出错,因此将您的css代码合并到源代码中:

<style type="text/css"> 

.highlight{               
background-color: #C0DDE0;     
}
</style>

and you should be able to something like this

你应该能够做到这样的事情

     <table id="table1" class="highlight"/>

#2


If the css still doesn't work even after you have placed a link-tag directly in the head, i'd say that there isn't anything wrong with the way you include the css, but rather the css-files themselves.

如果即使你已经直接在头部放置了一个链接标签,css仍然不起作用,我会说你包含css的方式没有任何问题,而是css文件本身。

Try including them all without the @import directly in the head. That way you can rule out any issues with the import.

尝试将它们全部包含在内,而不将@import直接放在头部。这样您就可以排除导入的任何问题。

#3


Have you verified that the tag is still on the page after a postback? I seem to remember something about difficulties manipulating the HEAD tag reliably.

您是否在回发后验证了标签是否仍在页面上?我似乎记得有关可靠地操纵HEAD标签的困难。

#4


Hmm, is there any reason why you reference your CSS files like this?

嗯,你有没有理由像这样引用你的CSS文件?