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;”);

or

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

Advertisements

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”;
    Page.Header.Controls.Add(css);

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s