Friday 30 November 2007

Dynamically change the cssclass of a div

I can never remember how to do this, so it's definately worth documenting.

You can add runat="server" and an id tag to any control, and you'll be able to access it from the code-behind, just don't expect to get at all of the attributes that the HTML version presents.

In my case, you can't edit the CSS class of a div just by adding an id and a runat="server" tag.

It turns out that you can, but it's not as direct as you may think. Most of the time properties like that come up by default, but not divs! For divs, you have to add the attribute manually:

myDiv.attributes("class") = "myclass"

If you need to do it for a bind method, you can cast the control to a htmlgenericcontrol.

All I can is that it's easy when you know how.