0

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");
2
  • where exactly do you try to set height attribute? (OnLoad?) How did you check that new value was not applied? (Dev tools, Firebug?) Commented Aug 26, 2014 at 11:53
  • I try to set height attribute in a button press event. I run the site in browser and the div has same height (150px). Thanks Commented Aug 26, 2014 at 11:58

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

1 Comment

You should also be able to just assign the class to the div using div.Attributes.Add("class", "nameOfDivClass"); This would be a cleaner approach.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.