views:

1509

answers:

4

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.

    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.

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

+1  A: 

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.

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:

<style type="text/css"> 

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

and you should be able to something like this

     <table id="table1" class="highlight"/>
TStamper
Within the head Tag, I have included a link reference to the CSS file. Same Issue. Which is really really weird
TimLeung
so with the link reference it works the original fresh of the page but after a postback it doesn't work anymore is what you're saying?
TStamper
Actually, I redid this again, if I have it included on the page, every single CSS file, then it works as intended. It has something to do with @import and IE7
TimLeung
A: 

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.

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

ullmark
A: 

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.

x0n
A: 

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