Solved

Listbox - Multiselect - Best Practices / Examples

Posted on 2004-09-16
3
507 Views
Last Modified: 2006-11-17
I have a general question concerning insertion of multi-selected items from a listbox - specifically as to the best method to identify the multiple selections and what the best method to store them in a SQL table (via CSV string or in a separate / dedicated table, etc.)?  Examples would be greatly appreciated!!!  Additionally, would be interested in how I can retrieve the results from the SQL db and set the selected values in a Listbox, within a Datalist - any help would be greatly appreciated!!!!!!
0
Comment
Question by:tbaseflug
  • 2
3 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 12076645
As far as identifying selections, just iterate through the listitems of the listbox on postback, and get the selections, there is really no easier way.

Storing the data is a little bit more tricky, let me try to steer you from making a mistake.  Realize if you store their selections in a csv form you break the first rule of normalization in table design.  Ideally you'd want a separate table for selections that relates to the primary table.  When it comes to updating their selections, I'd first delete all the old ones, and insert a record for each of their new selections.

If you do it like this, here is where it is a bit easier, updating their selections back to the listbox.  You can design a sql query to outer join two tables (all the items you want in the listbox, and the selected items they have chosen), that way you'll know if they are selected or not, then instead of setting datasource/databind, add the items manually to the listbox, and if from the query you can determine that they were selected, add the listitem as selected.  Make sense?

--Michael
0
 

Author Comment

by:tbaseflug
ID: 12077009
raterus -

That does make sense - is there any way that you could provide an expample on how to ("add the items manually to the listbox") - assuming that the values will bve coming from a sproc?
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 12077513
Well it really doesn't matter where they come from, once you get them simply iterate through them and do something like this. (using a SqlDataReader)

while rdr.read()
  Dim li as listitem = new listitem
  li.text = rdr("textfield")
  li.value = rdr("valuefield")
  if cbool(rdr("selected") = true then
    li.selected = true
  end if

  myListBox.Items.Add(li)
End while

This is assuming you've correctly created a result set that already has "selected" if they selected it or not.

--Michael

0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question