views:

6062

answers:

6

I need to change the height of a div container(CSS Property Height) from ASP.NET code (VB).

How can I do that?

Thanks

+8  A: 

C#, because I don't want to typo the VB syntax.

Markup:

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

Class of the Page:

protected System.Web.UI.HtmlControls.HtmlGenericControl divControl;

OnLoad/Other function:

divControl.Styles.Add("height", number / anotherNumer);
Tom Ritter
control you mean the ID of the div?
David Bonnici
Thanks anyway, but i dont knwo c# syntax....
David Bonnici
What's C# again :-)
Peanut
@David, even if you don't write in C#, it would probably be helpful to at least learn how to read and translate it. I think you'll find that many examples for new stuff from Microsoft will come out first in C#.
tvanfosson
@tvanfosson - Agreed, it's an excellent skill to have
Gavin Miller
@AviewAnew - In your example changing the code under the class section to "Dim divControl as System.Web.UI.HtmlControls.HtmlGenericControl" will fit VB.NET
Gavin Miller
A: 

As a NOT TO DO - Another way would be to use:

divControl.Attributes.Add("style", "height: number");

But don't use this as its messy and the answer by AviewAnew is the correct way.

Peanut
A: 

If your div is an ASP.NET control with runat="server" then AviewAnew's answer should do it. If it's just an HTML div, then you'd probably want to use JavaScript. Can you add the actual div tag to your question?

Eugene Katz
+1  A: 

VB Version:

Class:

Protected divControl As System.Web.UI.HtmlControls.HtmlGenericControl

OnLoad/Other function:

divControl.Style("height") = "200px"

I've never tried the Add method with the styles. What if the height already exists on the DIV?

Lurker Indeed
A: 

I find that code gets messy fast when C# code is used to modify CSS values. Perhaps a better approach is for your code to dynamically set the class attribute on the div tag and then store any specific CSS settings in the style sheet.

That might not work for your situation, but its a decent default position if you need to change the style on the fly in server side code.

AndrewDotHay