Question

I'm creating DIV controls from code behind using this code -

System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
    new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "dynDivCode";
dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
dynDiv.InnerHtml = "I was created using Code Behind";
this.Controls.Add(dynDiv);

Once created, I want to access it's height and width. Is it possible to do that? Or has it not been rendered on the screen at this point to have its properties accessible?

EDIT: I want to access the newly created div's(DIV1) width, but I also want to change the width of another div(DIV2) control i created based on DIV1's width. So I want to read and modify the width of 2 different div tags

Was it helpful?

Solution

There isn't a "width" established yet; it will depend entirely on how it's rendered by the client. You can get/set a CSS "style" attribute, or get/set a CSS class that will correctly style the element when it's rendered.

you should be able to accomplish what you're trying to do with CSS, but if all else fails, you could use Javascript (jQuery) to get the rendered size and use it to resize the other DIV.

OTHER TIPS

I would add a .css file and then use percentages to get a good UI Let's say we know them by its id's div1 and div2

Your classes on the .css file should look like this

#div1 {
    width: 55%;
    height: 50%;
}

#div2 {
    width: 45%;
    height: 50%;
}

and then, wrap them inside another div, lets call it wrappedDiv and use pixels in order to set the size correctly, add this other class in the .css file.

#wrappedDiv {
    width: 1024px;
    height: 768px;
}

Remember to add the .css file on the <head> tag of the page

<link href="~/Content/myNewStyles.css" rel="stylesheet" type="text/css" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top