Solved

Listbox - Multiselect - Best Practices / Examples

Posted on 2004-09-16
3
510 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 45
Checking a checkbox based on SQL DataReader boolean value in ASP.NET(VB) 2 45
asp.net web application 3 44
Why use this lambda? 12 60
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …

685 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