user2972061 user2972061 -4 years ago 51
ASP.NET (C#) Question

How to render CSS from code behind

Suppose I have a string which has CSS classes, I have to render it on page load.

My string value is:

.Class1 { display: none; } .Class2 { display: none; }

Answer Source

In your ASPX page, define a Literal control like e.g.:

<asp:Literal runat="server" ID="MyCss" />

Then, in the Load event handler of your code-behind, set the text like e.g.:

protected void Page_Load( object sender, EventArgs e )
    MyCss.Text = ".Class1 { display: none; } .Class2 { display: none; }";

Depending on your usage scenario, you would nest your Literal control instance inside other HTML or server controls.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download