Introduction

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

First off, you don't need to use the WebControl ListBox. Instead, you need to use the HTML Control ListBox.

You need to right click on the ListBox and select "Run as server control" so that you may use this ListBox from code behind.

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:

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".

History

Version 1.0 - 8/27/2004.

License

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.

Share

About the Author

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.

Unfortunately you are dealing with ASP.NET which in turn leaves you developing in a stateless environment. That basically means that .NET won't keep up with some things from postback to postback. If I remember correctly I kept up with all the selected items myself when I needed this solution to work. Sorry I couldn't have given you the easy answer but it's all I got for now

Well there is the Viewstate, which stores the other values for you, but the CSSStyle values are lost. Thanks anyway, I finally found a different solution: I stored the background values in a TextBox. This way I could save it to the server.

These are key items that must be in place:see the complete function below. Works great for me. Just remember, you have to call this function on postback.__________________________________________________________________________1. Session("listhold") = ListBox1.SelectedIndex2. If ListBox1.Items.Count &amp;gt;= 0 Then Try If Session("listhold") &amp;gt;= 0 Then ListBox1.SelectedIndex = Session("listhold") Else ListBox1.SelectedIndex = 0 End If