Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change CSS class of a HTML page element using ASP.NET?

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!

like image 940
Alexander Prokofyev Avatar asked Sep 23 '08 12:09

Alexander Prokofyev


People also ask

How do you change the class of an element in CSS?

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.

How do you change the class of an element in HTML?

To change all classes for an element:document. getElementById("MyElement"). className = "MyClass"; (You can use a space-delimited list to apply multiple classes.)

How can we call CSS class in code behind in asp net?

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.

What is CSS class in asp net?

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" .


1 Answers

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";
like image 87
user49845 Avatar answered Sep 18 '22 23:09

user49845