I need to change the background of a div based on some count property set in the code behind. I can get it to work as below. But i’m wondering if this is a good practice. If I need to change the count logic, I have to update code vs html. I suppose Jquery can’t do this unless I have a hidden variable with count on the page. Is this better than writing an If loop logic within the html?
html
<div class="show <%=CSSClass %>"> value </div>
code behind
public string CSSClass
{
get
{
if (count > 1) return "bright";
else if (count == 0)
return "normal";
return "dim";
}
}
That’s a good practice. I would stick with it. If you wanted to do it with jQuery you could expose the
countserver side property to the client and perform the same logic on the client. Note however that because this is done on the client there might be some delay after the page has rendered and the jQuery code has assigned the proper classname, so you could observe a flickering when the actual CSS class is applied to the DOM element on the client side.