1

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";
                }
            }
4
  • Aside: that's a strangely structured if else if. Did you really intend for "dim" to be mapped to count == 1 || count < 0? Commented Sep 1, 2011 at 20:15
  • if count > 1, return bright; if count = 0, return normal; else (if count < 1), return dim. I didn't have the == before, I updated it. Commented Sep 1, 2011 at 20:24
  • What if the count == 1? You current code returns "dim". Commented Sep 1, 2011 at 20:26
  • Yeah, i agree. this is just meant for illustrative purpose. First condition should really be if(count >=1) in this case. Commented Sep 1, 2011 at 20:34

3 Answers 3

1

That's a good practice. I would stick with it. If you wanted to do it with jQuery you could expose the count server 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.

Sign up to request clarification or add additional context in comments.

4 Comments

but this way, if have to change the count condition or css class names, i need to do a release. is that avoidable?
@Alex J, what do you mean that you have to do a release? I don't understand your question. You will simply output the count variable in your javascript: var count = <%= count %>;. So if you change the count value on your server the javascript count variable will obviously be updated as well.
Sorry, thats not what I meant. I meant if the logic/condition of calculating the css class changes, i will need to update code vs updating aspx html.
If you must have it be embedded in the aspx for changing on the fly, you can use the ? conditional operator to significantly shorten it: <div class="show <%= count > 1 ? "bright" : count == 0 ? "normal" : "dim" %>">
0

Class names are a perfectly valid way to do this, so is your technique of doing it on the server.

Comments

0

If your trying to use the MVC methodology there is no need for this.

the point in MVC is to make sure that you have no buisness logic in your view, and since this not logic of that type it seems to complicated, I would put the if in the page.

Unless of course you are gonna use this alot and then this would be perhaps not a bad idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.