简体   繁体   中英

Edit CSS code dynamically in ASP.NET

I'm new in ASP.NET and i have a little problem . I have a master page which has a div and i want to edit the height of this div dynamically with code. I don't want to change the href to another css file , i just want to edit this css file.

<div id="div" runat="server" ></div> 


#div
{
position:absolute;
background-color:red;
width:200px;
height:150px;
}

I tried this , but doesn't work :

      System.Web.UI.HtmlControls.HtmlGenericControl div;

      div.Style.Add("height","200px");

This should do the trick...

If you're writing this code in the code-beind of the master page you can just write the following:

div.Attributes.Add("style", "position:absolute;background-color:red;height:200px;height:150px;");

Otherwise, if you're on a content page, you'd add this before the code above...

var div = (System.Web.UI.HtmlControls.HtmlGenericControl) Page.Master.FindControl("div");

Although Scottys answer is correct, a nicer way might be the following:

div.Attributes.CssStyle.Add("height", "200px"); 

This will allow you to change individual css attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM