Setting css style programmatically in Asp.Net/C#

Sometimes we have requirements such a way that we need to change the elements and accordingly style dynamically under  div.

Suppose in one case  we need our div to be of width-size 800px and and can scroll if goes beyond.

for that on the front-end side (aspx) create <div id=”dvDStyle” runat=”server”>  </div>

then in code-behind  ( cs file) mention style using either of the way

1st one is mention it as Attribute for dvDStyle

2nd is mention style as separate property like Key,Value pair.

dvDStyle.Attributes.Add(“style”, “width:800px; overflow:scroll;”);


dvDStyle.Style[“width”] = “800px”;
dvDStyle.Style[“overflow”] =”scroll”;


One thought on “Setting css style programmatically in Asp.Net/C#”

  1. you can also do
    HtmlLink css = new HtmlLink();
    css.Href = “/file.css”;
    css.Attributes[“rel”] = “stylesheet”;
    css.Attributes[“type”] = “text/css”;

Leave a Reply

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

You are commenting using your 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 )


Connecting to %s