Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change CSS class of an element on runtime

Tags:

css

asp.net

Inside a repeater's ItemTemplate there is a:

<tr class="class1">

</tr>

I want this class to be changed to "class2" according to a valu that is bounded to this repeater, Eval("Locked").

If locked==true class="class1" else class="class2", how can I do it in simple way?
(in code behind it's to complex)

like image 945
samuel Avatar asked Oct 19 '09 08:10

samuel


People also ask

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

getElementById() method is used to return the element in the document with the “id” attribute and the “className” attribute can be used to change/append the class of the element. Syntax: document. getElementById('myElement').

How do you dynamically change CSS?

If you want to change the CSS styles dynamically you'll have to attach this portion of code to some event. For example, if you want to change the styles of your element when clicking on a button, then you have to first listen to the click event and attach a function containing the previous code.

How do I change a dynamic class in CSS?

In this article, we will see how to dynamically create a CSS class and apply to the element dynamically using JavaScript. To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript.


1 Answers

Really simple, just put a serverside tag:

<asp:Repeater ID="yourRepeater" runat="server">
    <ItemTemplate>
        ....
        <tr class='<%# Convert.ToBoolean(Eval("Locked")) ? "class1" : "class2" %>'>
            ....
        </tr>
        ....
    </ItemTemplate>
</asp:Repeater>

UPDATE: Thanks Kobi, i've missed Convert.ToBoolean() :)

like image 167
tanathos Avatar answered Sep 23 '22 17:09

tanathos