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;
