Internet Explorer's Conditional Formatting is some very cool stuff. Well, conditional formatting is a bit of a mis-nomer - it's actually just conditional comments, leveraged for css styling.
It makes styling documents tons easier, and results in cleaner CSS than a single page with all sorts of CSS Hacks in place. Employed for CSS Styling, Conditional comments let you 1) style a document/site in FF and then account for any abnormalities by providing a specialized Stylesheet for Internet Explorer (even different versions if needed).
Example:
With this main.css is applied to just about everything, but any boogers are sponged-up by a specific IE 6 or IE 7 set of overrides.
The problem though, is how do you do this, if you need to do it in ASP.NET, and also want the location of these stylesheets to be dynamically generated? You could try stuffing an <asp:Literal> control up in your header, but red-squiggles let you know that it's not a cool idea (even though it actually works at run-time):
So.. I spent a few minutes tonight looking at the new Page.Header control - which you get a reference to if you set <header runat="server"> in your markup. But it didn't have any real love - i.e., no methods for injecting comments or other things (though you can change the page's title, inject meta-data, and inject style info/references if you'd like).
So, I just decided to try dynamically creating a Literal control on the server and stuffing it in to the Header's .Controls collection. It worked perfectly.
(Using a GenericHtmlControl might also be an option, but I didn't play around with it long enough to figure out how to get around the fact that it really wants to use tagnames... )
Please consider using the @"xxx" form of string literals, which allows embedded newlines, you can also use single quotes instead of double quotes for attributes. Your init would be much clearer thus:
string conditionalText = @"
Line One
rel='stylesheet'
Line Three";
(Your won't let even escaped HTML-stuff but you get the point).
Posted by: Marc Brooks | November 10, 2006 at 11:11 AM