Change ListView row background

D

DavidC

I have a listview in an asp.net web page that I want to change the BackColor
when a condition is met. I am checking for and finding the condition in the
ItemDatabound event but I don't know what method I need to set the BackColor
on a single row. I have done it in a GridView using the GridViewRow but I
cannot find anything similar in a ListView control. Thanks.
 
G

Gregory A. Beamer

DavidC said:
I have a listview in an asp.net web page that I want to change the
BackColor
when a condition is met. I am checking for and finding the condition in
the
ItemDatabound event but I don't know what method I need to set the
BackColor
on a single row. I have done it in a GridView using the GridViewRow but I
cannot find anything similar in a ListView control. Thanks.

A list view does not have a row concept inherently, so you are setting up
the row "condition" in your code somehow (or tagging an item to look more
like a "row"). If you have not left any hooks to change background color,
I.E. some type of container (div, table cell, span, etc), then you have no
means of turning something black. As long as there is a cell being output,
you have an opportunity to do something.

Assuming you are setting up a table to create the concept of a "row" in a
ListView (I am not sure why no GridView here, but I am sure you have a
reason), there are a couple of ways to hack at this.

1. In the data binding method, grab inner controls until you find the
container in question (table cell, div, span, etc). There are two things you
can do then.

a) dynamically add a backcolor property
b) add information to match a CSS style (class is fairly easy)

Of the two options, I would choose B.

2. Alter the flow so you dynamically output the HTML. If you invest at this
level, it is probably better to create your own custom control (derived from
whichever control is best for you, ListView or GridView?).

I am not as fond of this option unless you have a good case for reuse of the
methodology. If this is a single page that you are doing this on, it is a
lot of work for a page. But, if you are using this methodology (conditional
painting of background), this option is very useful.

I would also consider, whichever route you go, that you make it so the
condition can be met asynchronously and still paint (AJAX), which pretty
much pushes you to the CSS methodology, as it is much easier to swap out CSS
classes on the client side than whack into properties. At least that is my
personal feeling on the matter. It also helps separate the content from the
formatting, which means you can easily alter things going forward. It may be
a paradigm shift for you.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Change ListView row background color 1
Add Lines to ListView 2
ListView control 4
Footer for ListView 0
ListView Insert 4
DropDown in ListView 3
Hide column in ListView 1
Getting unbound ListView data values 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top