Click here to Skip to main content
13,301,664 members (72,977 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 30 Aug 2004

Coloring items in a ListBox

, 30 Aug 2004
Rate this:
Please Sign up or sign in to vote.
This article describes the "bug/feature" in the ASP.NET ListBox.



This is my first article and it's a very simple one at that. However, I felt that it was necessary to post it because when I need to find an answer to a programming question, I always come to CP first, and then if I can't find my answer here, I hit Google. A few days ago, I needed to do some coloring in a ListBox on the web. I found that due to a known "bug/feature" by Microsoft, I couldn't do this. (KB Article.)

So, here are the steps to work around this bug in VS.NET 2003.

Creating your ListBox

  1. First off, you don't need to use the WebControl ListBox. Instead, you need to use the HTML Control ListBox.
  2. You need to right click on the ListBox and select "Run as server control" so that you may use this ListBox from code behind.
  3. Set the following properties (optional): ID=ColorListBox1 and Size=8.

You now have your ListBox created but it's not very useful yet now, is it? Next, we will load it up with some data and change some colors.

Loading the ListBox with data

I'm not going to do any databinding although you can. This ListBox now works just like the normal WebControl ListBox as far as I can tell. Go to your code behind and edit the Page_Load event. Add the following lines of code:

ColorListBox1.Items.Clear(); //Clears the contents of the ListBox 

ListItem liRed = new ListItem("Red", "Red"); //Create a Red item
     "background-color: RED"); //Make the back color Red

ListItem liBlue = new ListItem("Blue", "Blue"); //Create a Blue item
     "background-color: BLUE"); //Make the back color Blue

ListItem liGreen = new ListItem("Green", "Green"); //Create a Green item
     "background-color: GREEN"); //Make the back color Green

//Add the items to the ListBox
ColorListBox1.Items.AddRange(new ListItem[]{liRed, liBlue, liGreen});

Now you're done. If you view your aspx page, you should now see the same thing as the picture above.

Points of Interest

The only real point of interest is that Microsoft has a KB article about this, and in that article they say this bug is "by design".


Version 1.0 - 8/27/2004.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Richard Parsons
Web Developer
United States United States
Richard is a contract developer that currently lives in Arkansas. He has spent most of his career working in healthcare related companies. On his first job he was programming asp using VBScript and his boss walked in and told the whole group to get in their cars and drive to Barnes and Nobles. Once there he let everyone buy a few books about .NET and then he dropped the bomb, he said we would no longer write asp code. Starting Monday we were to be .NET developers.

Five years later Richard is a competent and self motivated .NET developer that can tackle most any problem. He has recently started his own business BellaDev which is proving to be a fun and challenging opportunity.

You may also be interested in...

Comments and Discussions

QuestionCan the code work in express edition Pin
Txstyle20-Jan-13 11:23
memberTxstyle20-Jan-13 11:23 
AnswerRe: Can the code work in express edition Pin
Zephlon23-Feb-13 13:07
memberZephlon23-Feb-13 13:07 
GeneralInfo lost on postback Pin
wekrato8-Mar-06 3:41
memberwekrato8-Mar-06 3:41 
AnswerRe: Info lost on postback Pin
Richard Parsons9-Mar-06 4:09
memberRichard Parsons9-Mar-06 4:09 
GeneralRe: Info lost on postback Pin
wekrato9-Mar-06 4:18
memberwekrato9-Mar-06 4:18 
GeneralRe: Info lost on postback Pin
Redfyre8-Oct-09 5:14
memberRedfyre8-Oct-09 5:14 
QuestionHow can I wire event to this listbox? Pin
sinanju2-Feb-06 10:15
membersinanju2-Feb-06 10:15 
AnswerRe: How can I wire event to this listbox? Pin
wekrato8-Mar-06 3:40
memberwekrato8-Mar-06 3:40 
Questionwanna populate my listbox with database fields Pin
SanaKashif29-Sep-05 11:15
memberSanaKashif29-Sep-05 11:15 
Generalit doesn't work.... Pin
Dinao31-Aug-04 21:59
memberDinao31-Aug-04 21:59 
GeneralRe: it doesn't work.... Pin
Dinao31-Aug-04 22:05
memberDinao31-Aug-04 22:05 
GeneralRe: it doesn't work.... Pin
Richard Parsons1-Sep-04 4:20
memberRichard Parsons1-Sep-04 4:20 
GeneralSerendipity Pin
doctrane31-Aug-04 1:26
memberdoctrane31-Aug-04 1:26 
GeneralRe: Serendipity Pin
Richard Parsons31-Aug-04 6:46
memberRichard Parsons31-Aug-04 6:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 31 Aug 2004
Article Copyright 2004 by Richard Parsons
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid