views:

1430

answers:

3

I am using aspx. If I have HTML as follows:

<div id="classMe"></div>

I am hoping to dynamically add a css class through the code behind file, ie on Page_Load. Is it possible?

Thanks coders.

+5  A: 

If you want to add attributes, including the class, you need to set runat="server" on the tag.

    <div id="classMe" runat="server"></div>

Then in the code-behind:

classMe.Attributes.Add("class", "some-class")
Chris Haas
Thanks, I was sure it would be this simple.
DanDan
+1  A: 

If you're not using the id for anything other than code-behind reference (since .net mangles the ids), you could use a panel control and reference it in your codebehind:

<asp:panel runat="server" id="classMe"></asp:panel>

classMe.cssClass = "someClass"
Jason
Thank you for your input.
DanDan
you're welcome :)
Jason
A: 

controlName.CssClass="CSS Class Name";

working example below

txtBank.CssClass = "csError";

Anwar