We help IT Professionals succeed at work.
Get Started

how to get all value from a listbox in asp.net

504 Views
Last Modified: 2012-05-06
i have the little code below that has 2 listboxes, listbox1 has items in it and 2 has nothing. then when you hit button1 it will move an item from listbox1 over to listbox2,
button2 moves all items from listbox1 to listbox2,
button3 moves 1 item from listbox2 to listbox1,
 button4 moves all items from listbox2 to listbox1.
so then i hit submit. but when i try to show what items are there in listbox2 it still displaying as empty and listbox1 still has its original values. So my question is how do i keep the value to stay in listbox2 so i can retrieve it in the serverside. thank you guys
<html>
<head>
</head>
<script language="javascript">
function add()
{
   s1 = document.getElementById("ctl00$Body$ListBox1");
   s2 = document.getElementById("ctl00$Body$ListBox2");
   index = s1.options.selectedIndex;
   if ( s1.options.length &&
        index != -1 ) {
        oOption = document.createElement("OPTION");
        oOption.text = s1.options[index].text;
        oOption.value = s1.options[index].value;
        s2.options.add(oOption);
        s1.options.remove(index);
        s1.options.selectedIndex=0;
   }
}
 
function addAll()
{
  s1 = document.getElementById("ctl00$Body$ListBox1");
  s2 = document.getElementById("ctl00$Body$ListBox2");
  for( i=s1.options.length-1; i>=0; i-- ) {
        oOption = document.createElement("OPTION");
        oOption.text = s1.options[i].text;
        oOption.value = s1.options[i].value;
        s2.options.add(oOption);
        s1.options.remove(i);
  }
}
 
function remove()
{
   s1 = document.getElementById("ctl00$Body$ListBox1");
   s2 = document.getElementById("ctl00$Body$ListBox2");
   index = s2.options.selectedIndex;
   if ( s2.options.length &&
        index != -1 ) {
        oOption = document.createElement("OPTION");
        oOption.text = s2.options[index].text;
        oOption.value = s2.options[index].value;
        s1.options.add(oOption);
        s2.options.remove(index);
        s2.options.selectedIndex=0;
   }
}
 
function removeAll()
{
  s1 = document.getElementById("ctl00$Body$ListBox1");
  s2 = document.getElementById("ctl00$Body$ListBox2");
  for( i=s2.options.length-1; i>=0; i-- ) {
        oOption = document.createElement("OPTION");
        oOption.text = s2.options[i].text;
        oOption.value = s2.options[i].value;
        s1.options.add(oOption);
        s2.options.remove(i);
  }
}
</script>
 
<body>
<form name=f1>
<table><tr><td colspan=2>
<asp:listbox id="listbox1" runat="server">
    <asp:ListItem Value="1">hello</asp:ListItem>
    <asp:ListItem Value="2">2 hello</asp:ListItem>
    <asp:ListItem Value="3">3 hello</asp:ListItem>
</asp:listbox>
</td><td colspan=2>
<asp:listbox id="listbox2" runat="server"></asp:listbox>
</td>
</tr>
<tr>
<td><input type=button value="Add" onclick="add()"></td>
<td><input type=button value="AddAll" onclick="addAll()"></td>
<td><input type=button value="Remove" onclick="remove()"></td>
<td><input type=button value="RemoveAll" onclick="removeAll()"></td>
</tr>
</table>
</form>
</body>
</html>

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE