views:

151

answers:

1

I have asp:Table with number of asp:Label inside asp:FormView, it represents short stats info.

I need to set Label.CssClass to "red" if it's text isn't "0".

Currently I do this on FormView.DataBound event. But think that it's better to use JavaScript and probably jQuery. How can I do that?

Sorry for dummy question - I'm new to jQuery. Thanks!

+1  A: 

You can do this using jQuery (you can also give the Table or FormView a class, probably easier in aps.net instead of the ID like I have below):

$("#formViewOrTableID span").filter(function() {
  return $(this).text() !== "0";
}).addClass("redClass");

If you give the labels a class you want affected, say set all the Labels you want included to CssClass="styleMe", you could change $("#formViewID span") to
$("#formViewID span.styleMe") to be more specific.

Nick Craver
Thanks! And what is the most appropriate way to attach call of this function as on server-side event like `DataBound` or `Load`?
abatishchev
@abatishchev - Are you in an `UpdatePanel` or just need it to run when the page loads?
Nick Craver
No, I don't use. Yea, just want to try to load when page loads.
abatishchev
@abatishchev - jQuery provides this functionality, either inside `<script tyle="text/javascript"></script>` in your page, or in an external `.js` file, just take what I have above and wrap it in `$(function() { ...answer code here... });`, this will execute it when the page is ready on the client, no server-side code needed.
Nick Craver