I have several <li> elements with different id's on ASP.NET page:
<li id="li1" class="class1">
<li id="li2" class="class1">
<li id="li3" class="class1">
and can change their class using JavaScript like this:
li1.className="class2"
But is there a way to change <li> element class using ASP.NET? It could be something like:
WebControl control = (WebControl)FindControl("li1");
control.CssClass="class2";
But FindControl() doesn't work as I expected. Any suggestions?
Thanks in advance!
document. getElementById('myElement'). className = "myclass"; Example 1: In this code change the class of the button from “default” to “changedClass” using the onclick event which in turn changes the background color of the button from RED to GREEN.
To change all classes for an element:document. getElementById("MyElement"). className = "MyClass"; (You can use a space-delimited list to apply multiple classes.)
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.
ASP.Net CssClass is an abstract wrapper around the css "class" specifier. Essentially, for most intents and purposes, they are the same thing. When you set the CssClass property to some string like "someclass", the html that the WebControl will render will be class = "someclass" .
Add
runat="server"
in your HTML page
then use the attribute property in your asp.Net page like this
li1.Attributes["Class"] = "class1";
li2.Attributes["Class"] = "class2";
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