Click here to Skip to main content
11,496,146 members (38,695 online)
Click here to Skip to main content

Moving Items from one Listbox to another.

, 11 May 2007 105.4K 27
This Article explains how to move Items between two listbox
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

Introduction

This Article is usefull to move the list items between 2 listbox or combo or dropdown, it will also give an alert message if no items of the listbox are selected.

Background

This code can be used in any web applications, in the javascript part.

Using the code

Assumptions:

The name of Buttons are assumed to be btnMoveRight and btnMoveLeft

The name of the ListBox are assumed to be ListBox1 and ListBox2

Code for the javascript function:

function fnMoveItems(lstbxFrom,lstbxTo)
{
 var varFromBox = document.all(lstbxFrom);
 var varToBox = document.all(lstbxTo); 
 if ((varFromBox != null) && (varToBox != null)) 
 { 
  if(varFromBox.length < 1) 
  {
   alert('There are no items in the source ListBox');
   return false;
  }
  if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1
  {
   alert('Please select an Item to move');
   return false;
  }
  while ( varFromBox.options.selectedIndex >= 0 ) 
  { 
   var newOption = new Option(); // Create a new instance of ListItem 
   newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
   newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
   varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
   varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 
  } 
 }
 return false; 
}

code for calling the javascript function:
There are 2 methods of calling the javascript code, they are as under.
1.  Add this code in the pageload
btnMoveRight.Attributes.Add("onclick","return fnMoveItems('ListBox1','ListBox2')");
btnMoveLeft.Attributes.Add("onclick","return fnMoveItems('ListBox2','ListBox1')");
2.  Add this code in the HTML
<input type = "button" id = "btnMoveRight" onclick = "fnMoveItems('ListBox1','ListBox2')">
<input type = "button" id = "btnMoveLeft" onclick = "fnMoveItems('ListBox2','ListBox1'">

Points of Interest

Let me know if you have any queries.

History

New Article on AJAX to be added shortly. Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Partha G
Software Developer (Senior)
India India
Hi this is Partha Sarathy . G working as a .NET Developer in Verizon, Hyderabad.
Follow on   Google+

Comments and Discussions

 
QuestionMy Page_Load Isn't Able To Recognize The input Buttons Pin
Don Dang4-Nov-14 19:21
memberDon Dang4-Nov-14 19:21 
Questionmove item from firstlistbox to another listbox. Pin
BhanuRaja30-May-12 0:36
memberBhanuRaja30-May-12 0:36 
Questiondocument.all Pin
Bujar Tahiri23-Oct-11 3:41
memberBujar Tahiri23-Oct-11 3:41 
GeneralMy vote of 5 Pin
vidyashan18-Apr-11 21:08
membervidyashan18-Apr-11 21:08 
Generaltest Pin
pune15-May-09 23:28
memberpune15-May-09 23:28 
GeneralRe: test Pin
pune16-May-09 2:28
memberpune16-May-09 2:28 
http://www.codeproject.com/KB/sharepoint/WebPartLifeCycle.aspx

http://www.c-sharpcorner.com/UploadFile/sarav82/webpartlifecycle01102008061313AM/webpartlifecycle.aspx

http://www.developer.com/net/asp/article.php/3620316
[^]
GeneralRe: test Pin
pune18-Jul-09 3:29
memberpune18-Jul-09 3:29 
Generalvalue not apear in page submit Pin
sanjsy31-Aug-07 1:11
membersanjsy31-Aug-07 1:11 
GeneralError in coes Pin
amit_ksp19-Jul-07 7:31
memberamit_ksp19-Jul-07 7:31 
GeneralAlternative Pin
Cstruter4-Jun-07 23:29
memberCstruter4-Jun-07 23:29 
GeneralRe: Alternative Pin
fahdansari13-Jul-07 10:52
memberfahdansari13-Jul-07 10:52 
GeneralRe: Alternative Pin
Cstruter14-Jun-08 7:36
memberCstruter14-Jun-08 7:36 
GeneralAlternative Pin
jawaharraj8923-Feb-12 0:50
memberjawaharraj8923-Feb-12 0:50 
QuestionWheres the data?? Pin
jamiemcshan1-Jun-07 10:17
memberjamiemcshan1-Jun-07 10:17 
AnswerRe: Wheres the data?? Pin
Cstruter4-Jun-07 23:46
memberCstruter4-Jun-07 23:46 
GeneralRe: Wheres the data?? Pin
a_nderu@hotmail.com15-Jun-07 5:45
membera_nderu@hotmail.com15-Jun-07 5:45 
GeneralRe: Wheres the data?? Pin
Cstruter20-Jun-07 1:41
memberCstruter20-Jun-07 1:41 
GeneralI am not getting values in the Codebehind Pin
PragenshMPatel22-May-07 4:24
memberPragenshMPatel22-May-07 4:24 
GeneralRe: I am not getting values in the Codebehind Pin
Cstruter14-Jun-08 7:37
memberCstruter14-Jun-08 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 12 May 2007
Article Copyright 2007 by Partha G
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid