Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

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

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

0
StewSupport
Asked:
StewSupport
  • 3
  • 2
1 Solution
 
Bane83Commented:
The server does not recognize most changes made via javascript (various reasons but mainly for security).  Generally this information is maintained by writing values to a HiddenField as they're moved back and forth and then use the values in the hidden field on the server.
0
 
mkosbieCommented:
You're never telling the server what values are in what boxes.  When you submit the form, only one value is sent for each listbox, not the entire contents of the list.  That means you have three choices: 1) Use AJAX to tell the server every time you've made a change, or 2) Make the buttons server-side and only make changes there, or 3) Implement a special client-side tracking system that your server checks at postback.  If you need help with any of those three options, just ask.
0
 
StewSupportAuthor Commented:
can you show me option 3?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
StewSupportAuthor Commented:
i'm trying to do option 1 and everytime i try to an item, it works fine but if i moved multiple selected items it will fail. here's the code
 For i As Integer = 0 To ListBox1.Items.Count - 1 Step 1
            If ListBox1.Items(i).Selected = True Then
                ListBox2.Items.Add(ListBox1.Items(i).Text)      
                ListBox1.Items.RemoveAt(i)        
            End If            
        Next

     
0
 
StewSupportAuthor Commented:
well this solve the problem of removing
 For i As Integer = 0 To ListBox1.Items.Count - 1 Step 1
            If ListBox1.Items(i).Selected = True Then
                ListBox2.Items.Add(ListBox1.Items(i).Text)                
            End If            
        Next
        For y As Integer = ListBox1.Items.Count - 1 To 0 Step -1
            If ListBox1.Items.Item(y).Selected Then
                ListBox1.Items.RemoveAt(y)
            End If
        Next y
0
 
mkosbieCommented:
Here's some code for option 3 (the custom tracking system) using hidden fields.  You'll have to add two asp:HiddenField's to the page (hdnLB1 and hdnLB2) by the listboxs to use it.
//add this funciton to your js
function updateHdns() {
    var s1 = document.getElementById('ctl00$Body$ListBox1');
    var s2 = document.getElementById('ctl00$Body$ListBox2');
    var hdnLB1 = document.getElementById('ctl00$Body$hdnLB1');
    var hdnLB2 = document.getElementById('ctl00$Body$hdnLB2');
 
    hdnLB1.value = '';
    hdnLB2.value = '';
    if(s1.options.length>0) hdnLB1.value = s1.options[0].text;" & vbCrLf & _
    if(s2.options.length>0) hdnLB2.value = s2.options[0].text;" & vbCrLf & _
 
    for(i=1;i<s1.options.length;i++) hdnLB1.value += ',' + s1.options[i].text"
    for(i=1;i<s2.options.length;i++) hdnLB2.value += ',' + s2.options[i].text"
}
 
//then call it after each of your update functions (ie add, addall, remove, etc)
 
//add this code to your server side in the Page_Load event handler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
        Dim arrLB1 As String() = hdnLB1.Value.Split(",")
        Dim arrLB2 As String() = hdnLB2.Value.Split(",")
 
        listbox1.Items.Clear()
        If arrLB1(0) <> "" Then
            For Each i As String In arrLB1
                listbox1.Items.Add(i)
            Next
        End If
 
        listbox2.Items.Clear()
        If arrLB2(0) <> "" Then
            For Each i As String In arrLB2
                listbox2.Items.Add(i)
            Next
        End If
    End If
End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now