views:

946

answers:

2

I have 4 listboxes (lstStates, lstCounties, lstCities, and lstZipcodes). Here are a few constraints:

  • None of the listboxes are disabled.

  • Any listbox can be selected at anytime, meaning there is no specific order the user has to choose.

  • Filtering is forward and backwards. By this, I mean if a user selects a state from lstStates, it will filter lstCounties, lstCities, and lstZipcodes. If a user selects a zipcode from lstZipcodes, it will filter lstCities, lstCounties, and lstStates.

  • The listboxes allow multiple selections.

Each listbox is bound to a datatable to get its initial data. The datatable is retrieved from a sqlserver stored procedure. Each listbox has its own stored procedure, for example, lstStates has one called GetStates which returns one column (State) and the ListBoxes DataValueField and DataTextField are both set to State. Similar to lstStates, lstCities is bound to a datatable which gets one column from a GetCities stored proc which is city.

Another thing I want to point out is that I am connecting an ObjectDataSource to get the datatable.

+2  A: 

Already been asked: http://stackoverflow.com/questions/381396/what-is-the-most-efficient-way-to-filter-listboxes-based-on-selections-of-another

[edit] OK, what you need to do is add an event to each [myListbox]_SelectedIndexChanged event. When the selection is changed you'll need to refresh all the other listboxes based on those selections. I assume that this will need to be handled by the database, since linking States to ZipCodes any other way would be ugly. So presumably your data for States<-->Zips<-->Counties relationships is in your db somewhere.

So you'll need to have procs in your db (or LINQ middle layer) that get States by Zips and so on. On each selection changed event, send the new selection back to the db sproc and then rebind the listbox based on the return data. You should be able to make one sproc for each one that returns all states if no zip is passed in and so on. [/edit]

jcollum
Yes, I asked that question, but felt it wasn't descriptive enough for the best answer.
Xaisoft
So edit the question.
Robert C. Barth
I have noticed many times, users don't notice that a question has been edited.
Xaisoft
The big question is, does your db have tables that join States to ZipCodes? And Counties to ZipCodes? I'll assume that a City can only be in one State.
jcollum
Like above, I always put the edits in their own block. Maybe if you need to remove a part of the original put it in brackets: [removed, see below].
jcollum
Well, all the tables are there, but there can be cities with the same naame in multiple states.
Xaisoft
The problem I have noticed though is that if you are the original poster, many users tend to get angry when you try to add content to your question by Answering it and not putting it in a comment and not everyone reads the comments.
Xaisoft
You can mark your own answers as the answer now... http://blog.stackoverflow.com/2009/01/accept-your-own-answers/
Jeff Martin
"Well, all the tables are there, but there can be cities with the same naame in multiple states". OK, but does every city have a State FK? Sounds like there may be some db structure issues that need to be addressed.
jcollum
A: 

To clarify, on your initial load of the page, you are loading ALL zipcodes, ALL cities, ALL states and ALL countries?

This seems a bit cumbersome to me. This is the type of requirement I would question. (Granted I don't know that you didn't already question it or that some good answer came from it).

Jeff Martin
Good point, ALL cities is a hell of a lot of records. Requiring the user to select a ZIP or State first might really improver performance.
jcollum
Bah, typo. improver = improve.
jcollum
Yes, I am retreived ALL the zipcodes, states, cities, counties. I am just using these as an example, the actual data I am retreiving is much smaller, but felt using zipcodes, states, cities, and counties would make a better example since everyone knows what they are.
Xaisoft