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)
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').
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.
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.
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() :)
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