[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Listbox - Multiselect - Best Practices / Examples

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
tbaseflug
Asked:
tbaseflug
  • 2
1 Solution
 
raterusCommented:
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
 
tbaseflugAuthor Commented:
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
 
raterusCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now