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?
If you want to add attributes, including the class, you need to set runat="server" on the tag. Thanks, I was sure it would be this simple. @Tyler, no. This adds a new class name to the control.
If your div has runat="server" then you directly use this: divAllItemList. Attributes["class"] = "hidden"; Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM.
<! DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With