Dynamically sizing a DIV tag in C# and ASP.NET

Recently I ran across the requirement of needing to size a div tag around a table in order to have it scroll and space correctly.  The number of rows in the table change so I needed to shrink the div around the table.  You can actually make the DIV tag act as a server side control and change it's styles dynamically!  Here's How:

In your aspx markup, make the DIV tag run on the server and assign it an id.

<div id="MyDivTag" runat="server" style="width:1000px; height:300px; overflow:auto; z-index:55" onscroll="javascript:SetDivPosition()" >

In your code behind,  you can now access the div tag as a server side control and change it's style.  The code below will size the div height style according to the numberOfRows.  You can now fully control when you want a table inside the div to scroll and not to scroll.

public void AdjustTableHeight(int numberOfRows)
{
  MyDivTag
.Attributes["style"] = String.Format("width:1000px; height:{0}px; overflow:auto; z-index:55", numberOfRows* 25);

}

Happy Web Coding!

Ebook Download
View all
Learn
View all