DanDan DanDan - 1 year ago 109
ASP.NET (C#) Question

Adding css class through aspx code behind

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.

Answer Source

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")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download