Reputation: 81
Do you know how to transfer data from listbox to another listbox ? I have made one but it didn't stop submitting although I use a function to stop it from submitting .
You can look at my previous problem in this section cancel-submit-code-not-working
The problem is not yet been solved even though there's few people helped me and I tried all their suggestions and recommendations but I always get the same result so I decided to ask you guys if there is someone who can do this using a javascript .
I tried to use one but it's not working because the function is for and I'm using LISTBOX so I think that's the reason why since I'm new to ASP.net C# javascript I can't change the code because ... it's not English :-D hope you can change the code for me?
Here's the code for javascript
<script type="text/javascript">
function Listbox_transfer(Listbox_Orig, Listbox_Move) {
var moved = new Array();
var count = 0;
for (var item = 0; item < Listbox_Move.options.length; item++)
{
if (Listbox_Move.options[item].selected) {
var temp = document.createElement("OPTION");
temp.text = Listbox_Move.options[item].text;
temp.value = Listbox_Move.options[item].value;
var index = 0;
var currOpn;
while (index < Listbox_Orig.options.length && temp.text > Listbox_Orig.options[index].text)
{
index++;
}
if (index < Listbox_Orig.options.length)
{
currOpn = Listbox_Orig.options[index];
}
else
{
currOpn = null;
}
try
{
Listbox_Orig.Listbox_transfer(temp, currOpn);
}
catch (ex)
{
Listbox_Orig.Listbox_transfer(temp, index);
}
moved[count] = Listbox_Move.options[item].value;
count++;
}
}
if (moved.length > 0)
{
remove(Listbox_Move, moved);
}
}
function remove(Listbox_OrigRemoveFrom, items)
{
for (element in items) {
var index = 0;
while (index < Listbox_OrigRemoveFrom.options.length &&
Listbox_OrigRemoveFrom.options[index].value != items[element])
{
index++;
}
Listbox_OrigRemoveFrom.remove(index);
}
}
function addAll(Listbox_Orig, Listbox_Move)
{
var moved = new Array();
var count = 0;
for (var item = 0; item < Listbox_Move.options.length; item++)
{
var temp = document.createElement("OPTION");
temp.text = Listbox_Move.options[item].text;
temp.value = Listbox_Move.options[item].value;
var index = 0;
var currOpn;
while (index < Listbox_Orig.options.length && temp.text > Listbox_Orig.options[index].text) {
index++;
}
if (index < Listbox_Orig.options.length) {
currOpn = Listbox_Orig.options[index];
}
else {
currOpn = null;
}
try {
Listbox_Orig.Listbox_transfer(temp, currOpn);
}
catch (ex) {
Listbox_Orig.Listbox_transfer(temp, index);
}
}
removeAll(Listbox_Move);
}
function removeAll(list) {
for (var count = list.options.length; count >= 0; count--) {
list.remove(count);
}
}
function selectAll(Listbox_OrigSelect1, Listbox_OrigSelect2) {
for (var count = 0; count < Listbox_OrigSelect1.options.length; count++) {
Listbox_OrigSelect1.options[count].selected = true;
}
for (var count = 0; count < Listbox_OrigSelect2.options.length; count++) {
Listbox_OrigSelect2.options[count].selected = true;
}
}
I'm using input buttons
<input id="toTheRightButton" type="button" value=">>>" class="button button-primary" onclick="Listbox_transfer(this.form.ListBox3, this.form.ListBox2)"/>
Now here's the whole code of the demo that I tried to copy
<html>
<head>
<title>Lisbox Demo</title>
<script type="text/javascript" src="Listbox.js"></script>
</head>
<body>
<form method="post">
<table style="text-align: center" border="0">
<tr><td><strong>List 1:</strong><br/>
<select size="15" name="list1[]" id="list1" style="width: 350px" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</select></td>
<td>
<br/>
<input type="button" value=">>" onClick="add(this.form.list2, this.form.list1)"/>
<br/><br/>
<input type="button" value="<<" onClick="add(this.form.list1, this.form.list2)"/>
<br/><br/>
<input type="button" value="All >>" onClick="addAll(this.form.list2, this.form.list1)"/>
<br/><br/>
<input type="button" value="All <<" onClick="addAll(this.form.list1, this.form.list2)"/>
</td>
<td><strong>List 2:</strong><br/>
<select size="15" name="list2[]" id="list2" style="width: 350px" multiple="multiple">
</select></td></tr>
</table>
<p> </p>
<p><select name="list3" size="14" multiple>
<option value="0">selection1</option></select> </p>
</form>
</body>
As you can see the code is for option list .. not for listbox for asp.net :-( hope you can help me thank you .
Upvotes: 0
Views: 1011
Reputation: 1210
Following function is for single item. you can modify it for all items and also make it single by passing listbox id as parameter:
function Add() {
var selectedItem = $("#lbAvailable > option:selected");
if (selectedItem.length > 0) {
selectedItem.remove().appendTo("#lbSelected");
$("#lbAvailable option:first-child").attr("selected", true);
$("#lbSelected option:last-child").attr("selected", true);
}
else {
alert("Select item");
}
}
function Remove() {
var selectedItem = $("#lbSelected > option:selected");
if (selectedItem.length > 0) {
selectedItem.remove().appendTo("#lbAvailable");
$("#lbAvailable option:first-child").attr("selected", true);
$("#lbSelected option:last-child").attr("selected", true);
}
else {
alert("Select item");
}
}
function AddAll() {
$("#lbAvailable option").each(function () {
this.remove().appendTo("#lbSelected");
});
}
function RemoveAll() {
$("#lbSelected option").each(function () {
this.remove().appendTo("#lbAvailable");
});
}
Upvotes: 1