473,787 Members | 2,928 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Move items between listboxes

13 New Member
I'm having a hard time figuring out how to move items between a left and right listbox server-side.

I have a populated listbox from the database on the left and I'm trying to add items to the box on the right and save those values to the database.

Can anyone help me? I've searched everywhere and haven't found any code that's helpful!

Expand|Select|Wrap|Line Numbers
  1. protected void Submit_Click(object sender, EventArgs e)
  2.     {
  3.         //foreach (ListItem item in SecondList.Items)
  4.         // {
  5.         //   string sql1 = "INSERT into sch_relations_ref VALUES (@relation_id, @resource_id)";
  6.         //   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BayNetConnectionString"].ConnectionString);
  7.         //  SqlCommand command = new SqlCommand(sql1, con);
  8.         //  SqlParameter relation_id = new SqlParameter("@relation_id", SqlDbType.Int);
  9.         //  command.Parameters.Add(@relation_id);
  10.         //  SqlParameter resource_id = new SqlParameter("@resource_id", item.Value);
  11.         //  command.Parameters.Add(@resource_id);
  12.         //  con.Open();
  13.         //  command.ExecuteNonQuery();
  14.         //  con.Close();
  15.  
  16.         //string value1 = hid_Items.Value;
  17.         //Response.Write(value1);
  18.         // }
  19.     }
I've tried doing something like this, but when I hit the submit button, it never saves the list. I'm so lost.

Also, here's the javascript... I'm trying to see if there's a way to do it without JS maybe with Ajax, but I'm a beginner with Ajax.

Expand|Select|Wrap|Line Numbers
  1. // Begin -->
  2.     function move(fbox, tbox)
  3.     {
  4.         var arrFbox = new Array();
  5.         var arrTbox = new Array();
  6.         var arrLookup = new Array();
  7.         var i;
  8.         for (i = 0; i < tbox.options.length; i++) 
  9.         {
  10.             arrLookup[tbox.options[i].text] = tbox.options[i].value;
  11.             arrTbox[i] = tbox.options[i].text;
  12.         }
  13.         var fLength = 0;
  14.         var tLength = arrTbox.length;
  15.         for (i = 0; i < fbox.options.length; i++)
  16.          {
  17.              arrLookup[fbox.options[i].text] = fbox.options[i].value;
  18.              if (fbox.options[i].selected && fbox.options[i].value != "")
  19.               {
  20.                   arrTbox[tLength] = fbox.options[i].text; tLength++;
  21.               }
  22.               else 
  23.               {
  24.                   arrFbox[fLength] = fbox.options[i].text; fLength++;
  25.               }
  26.           }
  27.           arrFbox.sort();
  28.           arrTbox.sort();
  29.           fbox.length = 0;
  30.           tbox.length = 0;
  31.           var c;
  32.           for (c = 0; c < arrFbox.length; c++)
  33.            {
  34.                var no = new Option();
  35.                no.value = arrLookup[arrFbox[c]];
  36.                no.text = arrFbox[c];
  37.                fbox[c] = no;
  38.            }
  39.            for (c = 0; c < arrTbox.length; c++)
  40.             {
  41.                 var no = new Option();
  42.                 no.value = arrLookup[arrTbox[c]];
  43.                 no.text = arrTbox[c];
  44.                 tbox[c] = no;
  45.             }
  46.         }
  47.         function selectAll(box) 
  48.         {
  49.             for (var i = 0; i < box.length; i++)
  50.              {
  51.                  box[i].selected = true;
  52.              }
  53.          }
  54.          // End -->
Jun 9 '09 #1
5 5683
cloud255
427 Recognized Expert Contributor
Hi,

I suggest you look into using List rather than an array.
The list is a dynamic data structure which exposes the Add() and Remove() methods.

You can thus loop through your list and populate list 1 (left list)
when the user moves an item to list 2, you remove from your one list at the selected index of the item (or better use objects and pass a unique value to remove) and add that item to the other list.

Then re-bind the controls with the new lists.
Jun 10 '09 #2
amandab116
13 New Member
Is there any way you can provide an example? I was just thinking about this whole situation and I'm still stuck on the most logical way to deal with it. I've implemented hidden fields, but then I can't really save multiple values to the database.

Thanks!
Jun 10 '09 #3
cloud255
427 Recognized Expert Contributor
Hi

Create a class or struct to hold your data. Next create 2 lists of that data type:

Expand|Select|Wrap|Line Numbers
  1.  List<MyDataObject> collection1, collection2;

you can now bind the list as the data source for the listbox.

you can then call the add() function of the second collection to add a selected item:

Expand|Select|Wrap|Line Numbers
  1. collection2.Add(DataObject);
and the remove object to remove the selected item from the original list:

Expand|Select|Wrap|Line Numbers
  1. collection1.Remove(SelectedItem);
Now simply refresh the listboxes.
Jun 10 '09 #4
amandab116
13 New Member
Thank you very much!!
Jun 10 '09 #5
cloud255
427 Recognized Expert Contributor
@amandab116
Always a pleasure to help.
Jun 11 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
9528
by: Vincent Yang | last post by:
I'm using Access 2002. My database is in Access 2000 format. I'm designing a form to collect responses to a 40-item questionnaire. I can fit four list boxes on a screen, so I plan to have 11 tab pages (one page at the end for collecting identifying information) on my form. To ease the task for the user, I'd like to have an automatic movement to the next page. For instance, when the user clicks on the choice for Question 4 (the last of...
10
21901
by: SueB | last post by:
Hi. Is there a way to "unselect" items in a listbox, programmatically? Here's what I want to do (and I've been trying to to this while debugging the form, but I've been unsuccessful) ... I have a form with two listboxes. The form, also, has two option buttons in an options group: 1 button for
1
1596
by: kent | last post by:
Hi All, On my form, I have 2 listboxes that get populated with the correct items. I have 4 buttons between the 2 boxes. The first 2 buttons are used to move the selected item back and forth between the boxes. That works fine. The last 2 are used to move all the listbox items (whether they're selected or not) that are in 1 box over to the other box. After the move, I want the other box to be empty. I am calling clear() and
4
3711
by: Ryan Ternier | last post by:
Thanks for the previous help guys! I got my list box issue working, but now i'm trying to loop through all the items in my page. I want to find each listbox, once I do i strip the ID down to find what I need so I can populate a DB. lstTest is dim'd as a ListBox, and I thought I could just do:
2
2704
by: Srimadhi | last post by:
Displaying selected items at the top of the listbox Hi, I am having two listboxes - one with ids and second with the related names. When user selects an item in one listbox, the corresponding item will be selected in the second listbox too. The problem is, when the user selects an item and it is in the middle of the list, the user is not able to know whether it is selected or not. Is there a way to display all the selected items...
1
2948
by: anjupt | last post by:
Hi, I have 2 listboxes with data fetched from database so for both the listboxes datasource is set .i tried to move data from one listbox to another listbox but Error"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll Additional information: Cannot modify the Items collection when the DataSource property is set. " comes up .how can i move data from one listbox to another listbox Thank you
5
3759
by: SandeepJagdeva | last post by:
Hi All I am working on a project in which i have to face a problem. In my project I have Three listboxes and I want to store these listboxes Items in MS Access database. I don't know how to do this. can any one help me
4
4634
by: bogdan | last post by:
Hi, I have two listboxes on a page and need to move items between them - using buttons (e.g. "<<" ">>"). Can this be done on a client side in asp.net? I'd like to avoid hitting the server on every add/remove. Thanks, Bogdan
3
5050
JodiPhillips
by: JodiPhillips | last post by:
Hello everyone, there are many questions and answers relating to moving items between two listboxes here and on the net in general, however, none answer my specific problem. I have two listboxes on a form. The first listbox is populated according to command buttons (Command 14 & Command 15) that are clicked by the user (draws data via SQL statement - see code below). The second listbox is populated by user selection from the first listbox. I...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.