Different Background Colors for ListBox Items

  • Thread starter Thread starter gnosys
  • Start date Start date
G

gnosys

In ASP.Net 1.1 using C#, I'm trying to dynamically change the
background colors of certain listbox items based on some criteria. For
example:

[ Select Items ]
[Item 1]
[Item 2]
[Item 3 (Hot) ]
[Item 4]
[Item 5 (Hot ]

In the ListBox above, I would like the Items that are "Hot" to have a
red background. This box will obviously be populated from a dataset,
and my code time is like this:

foreach (myItem i in myItems)
{
ListItem li = myListBox.FindByValue(i.itemValue);
if (!i.IsHot)
continue;

if (li != null)
li.Attributes.CssStyle.Add("background-color", "yellow");
}

No Compilation errors, I have tried other ways to do this, didn't
work. However, if I just do ListBox.Attributes.CssStyle - it changes
the bg color of all the items. I only need to change the background of
certain items. Any help will be appreciated.
}
 
In ASP.Net 1.1 using C#, I'm trying to dynamically change the
background colors of certain listbox items based on some criteria. For
example:

[ Select Items ]
[Item 1]
[Item 2]
[Item 3 (Hot) ]
[Item 4]
[Item 5 (Hot ]

In the ListBox above, I would like the Items that are "Hot" to have a
red background. This box will obviously be populated from a dataset,
and my code time is like this:

foreach (myItem i in myItems)
{
ListItem li = myListBox.FindByValue(i.itemValue);
if (!i.IsHot)
continue;

if (li != null)
li.Attributes.CssStyle.Add("background-color", "yellow");
}

No Compilation errors, I have tried other ways to do this, didn't
work. However, if I just do ListBox.Attributes.CssStyle - it changes
the bg color of all the items. I only need to change the background of
certain items. Any help will be appreciated.
}

Check if your code actually causes any style to be set on the <option>
tags in the resulting code.

You should know that the support for styling a select is limited, and
varies from browser to browser. Unless you are building an intranet
where you can impose the usage of a specific browser, you should only
use the style as a visual aid, i.e. also change the actual text of the
item to make sure that all users can see the status.
 
Back
Top