Manipulate Div Tags in Code behind file in ASP.NET

I have 2 Div tags in my ASPX page and i need to set the height, width of the Div's Based on certain conditions..

How can i achieve this using a C# Code behind file?

What property needs to be set to do this.. I tried using the Div.Style Property but it only allows to apply css style sheets and not set Height, Width ..

Please Help
Who is Participating?
orbulatConnect With a Mentor Commented:
yes, u can set the div's width and height, just like the experts suggested above.

but it will be better for u to change the cssClass instead of hardcoding the height and width in codebehind, so next time if u have style updates, u don't have to compile the codes again.. and the updates can be done in front-end css rather than back-end codes..


if (condition1) {
   MyDivTag.CssClass = "class1";
} else {
   MyDivTag.CssClass = "class2";

.class1 {
    height : 100px;
    width : 100px;

.class2 {
    height : 200px;
    width : 200px;
You can use the style property:

MyDiv.Style("Height") = "100px";
MyDiv.Style("Width") = "200px";

For example.
Yes you can do so in your code behind.

Have a runat=server attribute and an ID for your DIV tag to create a
HtmlGenericControl on the server side. Then you can easily set the width or
height of the DIV tag on the server-side itself.

<DIV id="MyDivTag" runat="server"></DIV>

In your code-behind:

MyDivTag.Attributes["style"] = "width:100px; height:200px;";

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.