ASP.NET + ListView + if statment, change style

  • Thread starter Thread starter Izi
  • Start date Start date

Izi

Izi

Soldato
Joined
9 Dec 2007
Posts
2,718
Hi,

I have a listview, which is databound to a SQL data source.

I want to, depending on the retunred value of the SQL statment, change the colour of the TR row in my Item Template.

IE....
<ItemTemplate>
<tr>
<td>CHANGE THE BACKGROUN COLOUR HERE DEPENDING ON USER TYPE</td>
</td>
</ItemTemplate>

So my SQL statment pulls users from the DB. If they are Admin I want the background of the tr red, otherwise green.

Thanks for your help.
 
I would suggest one of two approaches:

- Unbind the control - manually create the ListView in code, and define the CssClass property each ListDataItem accordingly.

- Keep the bound control, and (by creating the DataBound event handler) iterate through the ListItems afterwards and adjust the CssClass proprties accordingly.

The former approach is arguably more efficient, but will require a little more code (but code that would be worth practising nonetheless). The latter approach means you can skip the creation of code to create & populate the listview, but it's a little less elegent.

There *may* be a way to define the class during the binding operation, but if there is, it is beyond my knowledge.

Hope this helps.
 
Back
Top Bottom