Embedding Dynamic CSS in Asp.Net

There are many questions on SO on dynamically generating or applying CSS in ASP.Net or ASP.Net MVC.

Today I accidentally found a simple way to embed dynamic CSS in an aspx file:

in the markup, add runat=”server” to the <style> tag

<style type=”text/css”runat=”server”id=”htmlCss”></style>

This will generate a field of type HtmlGenericControl in the page.

In one of the page life-cycle events (Page_Load, Page_Init, etc), assign the literal CSS definition like this:

var css = @"
body
{
  background-color:#b0c4de;
}";
htmlCss.InnerHtml = css;
Advertisements

2 Responses to Embedding Dynamic CSS in Asp.Net

  1. w says:

    Where do you get the htmlCss object?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: