A product I’m working on requires colours for certain items, to make them more easily identifiable and these items are often a selection in a list. This makes a great deal of sense as we are visual creatures; it’s much easier to pick out a red item from a list than having to read the text. In UI terms it may not look great, the colours glaring with the overall design, but that’s a point for another day.

So it seems sensible to have the list display those colours, but herein lies the problem. Or problems. The ListBox doesn’t have an OnItemDataBound method, so you can’t add the style attributes as each item is being bound. This leaves two choices:

  1. You could loop through the items in the DataBound event afterwards, adding the style attributes.
  2. Add the items manually, adding the style attributes to the ListItem before you add it.

Neither are particularly onerous; for this application there are only a few items, used on a few pages.

All looked rosy until postback when the styles just disappear; no jarring colours to help identify the items. A quick investigation reveals that the ListItem doesn’t persist the attributes as part of its ViewState, which means that although you can display the list items in colour, the colour disappears once you post back. There are ways to get around this, client side etc, but that’s just not an avenue I want to persist; the effort just isn’t worth it.

As it turns out I’m quite happy that it is a problem, because I don’t actually like the colours in the list itself; it really doesn’t look great. Now I’ve got a good excuse when the client says “why can’t the lists be in colour”.

Technorati Tags: ,