Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Freaky ListBox Problem

Status
Not open for further replies.

jfrost10

Programmer
Jun 3, 2001
2,004
CA
Hey guys,

Ok, this is really messing with my head. I have a list box. This list box has some items in it. If an item is clicked, it grabs subordinate items, clears the listbox, and re-fills it with the new items. Problem is, after the first fill, it ALWAYS sees index 0 as the selected index, even though you could click any other item! There is no code that explicitly sets the selected index to 0, so I am totally stumped. We've worked with listboxes before without any probs, and this just...stumps me! Any thoughts, or known listbox issues with postbacks, viewstates, etc.?

Thanks,

Jack
 
I had this same problem - the selection is always 0. You need to check your Page_Load function and make sure you have the following correctly placed:

If Not IsPostBack Then
..
End If

It worked fine for me after this. I spent a whole day ripping my hair out over this... Chris says: "It's time for a beer."
 
Chris,

Its been in there all along man, its something else.

Jack
 
Have you tried setting selectedIndex = -1 when the list is rebound? That's the default for no selection. Of course, you shouldn't have to do that, you're probably missing something in the postback code.. Chris says: "It's time for a beer."
 
Tried that too buddy, no go.

There's nothing really to miss: There's a "Fill List" sub, and if its not a postback (first load), it calls fill list with default entries. otherwise, its a postback, and Fill List has been called from whichever event fired.

Its just bloody strange!

Jack
 
Jack what exactly do you mean by always sees the selected index as 0? Are you running through using the debugger? Or are you unable to select another item in the list?

I had a problem sort of similiar in that when I tried to change the selected index programatically it would stick on zero. I managed to work around it by setting the selectedindex property rather than doing a .selected = true.

[peace]
 
Hey Zar,

Just that man: it always sees the selected index as 0.
And yes, we are using a debugger (which makes this even more interesting since we still can't figure out why its happening).

Here's the flow:
1. List box is filled with nodes from an xml document using the xpath objects
2. user clicks one of the items
3. Listbox's SelectedItemChanged event fires, which grabs the selected item and fills the list box with any child nodes of that item from the xml document (this first time does work if you click on an itme that isn't the first one)
Now here's where it messes up
4. User clicks one of the items in the listbox, NOT the first item
5. Listbox's SelectedItemChanged event fires, but ALWAYS sees the item as index of 0 (whether we look for the selectedindex or selecteditem, its both the 0-positioned item)

Thats the problem: first time, it works. After that, it keeps defaulting to 0. The listbox is set to autopostback, and viewstate is enabled.

Any thoughts?

Jack
 
Just to make sure I am getting this right. This first time the user selects an item the list box is loaded with completely different items?
My first guess would be that something is going wonkey in the reloading of the listbox. Unlikely, as loading a list box is fairly straight forward, but where I would look. Umm.. The only other thing I can think of is that the viewstate may for some reason or another be messed up.

Sorry I can't be of any more help, but I am kind of shooting in the dark without anything to look at. [peace]
 
np man, we're shooting in the dark too.

Worst case scenario, we have had some non-fully-tested success with putting the code to reload the listboxes in a seperate command button, so if we can't get the behaviour we want with just hte listbox, we'll probably run with that.

Just weird though, and is driving us nuts why a straight-forward thing is causing so much trouble.

Thanks again,

jack
 
very odd problem indeed.
If you find a solution please post it. I would like to know why this is happenning. [peace]
 
OH GOOD GOD!

k, we figured it out.

Apparantly, list boxes don't like it if multiple list items have the SAME VALUE! So if you have three items:
Text Value
Item1 0
Item2 0
Item3 0

even if you pick Item3, the selected item property of the list box will point to Item1!

HOW FREAKIN' GHEY! So apparantly, ListBoxes are NOT the control of choice if you want the users to select multiple items that have identical values.

Jack
 
o...

Well then that was rather dumb. Glad you figured it out.

Cheers. That'l do donkey, that'l do
[bravo]
 
Maybe you should throw this into a FAQ called "Listbox Hell" Chris says: "It's time for a beer."
 
LOL! Or maybe I should start a thread like paul (link9) did, but call it 'Tidbits FROM HELL'.

that has a nice ring to it to.
;)

Jack
 
I like the Tidbits FROM HELL idea. I'd say run with it That'l do donkey, that'l do
[bravo]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top