Welcome to U E T - C S . org Sign in | Join | Help

Osama Abbas

the Answers to Life, the Universe, and Everything
ASP.net ListBox woes ...

Today I was working on a WebUserControl that basically contained two ListBoxes. One contained a list of items available and the other had all the selected items. A problem came up where when I selected an item in the available items list and tried to copy it to the other list, the SelectedIndex reported by the ListBox would be wrong. I tried every trick I could to fix it but the mysterious problem persisted. What puzzled me even more was that I had made a similar control a few days back and it worked flawlessly !

After a couple of hours of trial and error, I stumbled upon the real cause of the problem :

Every Item in the list has two properties, called Text and Value. Text dictates what is to be shown in the ListBox and Value basically acts like an index value. What I observed was that in my case, the Text values were unique but the Values were repeating for a number of items. Given the current circumstances, it was allright to have similar values for multiple items. The problem lies in the ASP.net ListBox which does not like this kind of condition. I had to wrangle with multiple joins to use a different column to bind to the ListItem Value .. but this eventually solved my problem.

Moral of the story :
If your ListBox is acting weird, check your value bound column for repeating values.
 

Posted: Tuesday, April 03, 2007 4:20 AM by osama.abbas
Filed under:

Comments

fahad said:

Well this is not ASP .NET's issue :P .... this is issue with HTML representation of List Box.....

# April 12, 2007 6:38 AM

osama.abbas said:

wow ... someone actually reads my blog!

even if its just one person :P

I cant blame anyone though ...

why would a bunch of CS people be interested in a programming blog.

# April 12, 2007 7:00 AM

aliraza said:

i dont understand you are still having prob or you are just explainin de answer.. :) coz there seemz no problem  and hence no need to answer..???

# May 7, 2007 6:01 AM

osama.abbas said:

hmmm ... lets analyze the structure of the post :

Paragraph 1 tells how I was going my merry way and encountered an unexpected problem.

Paragraph 2 tells how I identified the cause of the problem.

Paragraph 3 provides a detailed description of the problem and then a solution is presented.

Paragraph 4 gives a "moral of the story" which states in one line a point to look out for if anyone else has the same problem.

So tell me what part of the above do you not understand ?

# May 7, 2007 6:13 AM
Anonymous comments are disabled