468,268 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

Different Background Colors for ListBox Items

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.
}
Oct 17 '08 #1
2 10806
See if this helps:
http://www.codeproject.com/KB/custom...?display=Print

//Relevant code:
ddlMultiColor.Items[row].Attributes.Add("style",
"background-color:" + ddlMultiColor.Items[row].Value);

<gn****@gmail.comwrote in message
news:f5**********************************@v13g2000 pro.googlegroups.com...
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.
}

Oct 17 '08 #2
gn****@gmail.com wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Oct 18 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Scott | last post: by
3 posts views Thread by Bob - Andover, MA | last post: by
16 posts views Thread by J. B. Moreno | last post: by
3 posts views Thread by Baren | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.