R – How to dynamically show/hide a style sheet inside a content control

asp.netmaster-pages

I have a master page with a contentplaceholder control inside the head tag so that I can easily add any contents to the head tag (like link tags, styles, scripts .. etc) from the content pages.

Now, on one of the content pages, I have a style sheet (inline, ie. inside a style tag, not linked with a link tag) that I want to show/hide depending on the state of the page (the page works like a wizard using a multiview control on the page). I tried to wrap the stylesheet inside a placeholder control with Visible = false, then set Visible to true in the code but that didn't work, the style tag is not rendered in the html. I removed the placeholder control and added runat="server" directly to the style tag but that didn't work too.

The reason I keep the styles hidden in the first place is that they contain formatting placeholders, ie. {0}, {1} ..etc, which I replace in the code behind before showing the stylesheet. I don't want to send the styles to the client with these placesholders. To make it clearer, the styles look similar to this:

<asp:placeholder ID="_stylesPlaceHolder" runat="server" Visible="false">
<style type="text/css">
#elementid
{
  width: {0}px;
  height: {0}px;
  overflow: hidden;
  position: relative;
}
#elementid2
{
  position: absolute;
  width: {1}px;
  height: {2}px;
  top: {3}px;
  left: {4}px;
}
.. and so on

</style>
</asp:placeholder>

I could try to add the styles dynamically from the code behind page (using Page.Header.StyleSheet.CreateStyleRule for example) but I prefer to keep them in the .aspx file for the sake of separating the html from the code and also to make it easier to change the styles with no dynamic values.

Thanks

Best Answer

OK, now I finally got it. First of all it's nothing wrong with ASP.NET. I was testing with google Chrome when I posted the question. As I indicated in my comment above, I tested with FireFox afterwards and found the style tag was actually there.

I checked again in Chrome but this time I checked in the code inspector instead of the page source, the style tag is also there. Chrome didn't like the style tag with the placeholders (I was testing so I still didn't replace the placeholders) and decided to hide it from the page source view and push it under the rug! Good boy!!

Related Topic