benjamin54
benjamin54

Reputation: 1300

Moving items between Listboxes

I'm having two list boxes on my page, & I need to move items between them with the help of javascript. This is my markup:

<asp:ListBox ID="ListBox1" Height="300px" runat="server" AppendDataBoundItems="true" 
        SelectionMode="Multiple"></asp:ListBox>

<div>

<asp:ImageButton ID="ButtonRight" runat="server" ImageUrl="~/Images/right.gif" OnClientClick="return     
      LeftToRightMoveItems('AddSetup');" /><br />
<br />
<asp:ImageButton ID="ButtonLeft" runat="server" ImageUrl="~/Images/left.gif" OnClientClick="return 
       RightToLeftMoveItems('AddSetup');" />
</div>

<asp:ListBox ID="ListBox2" Height="300px" runat="server" AppendDataBoundItems="true"  
      SelectionMode="Multiple"></asp:ListBox>

Here's my javascript code:

Javascript:

 function LeftToRightMoveItems() {
        try {
            if (status == "AddSetup") {
                var varFromBox = document.getElementById("<%=ListBox1.ClientID%>").options;
                var varToBox = document.getElementById("<%=ListBox2.ClientID%>").options;
            }

            alert(varFromBox.length);
            alert(varToBox.length);

            if ((varFromBox != null)) {
                if (varFromBox.length < 1) {
                    alert('There are no items to move!');
                    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
                }
            }
        }
        catch (e) {
            alert("Following error occured : \n" + e.description);
        }
        return false;
    }

On page load, I'm filling items in ListBox1. But on alert() I'm getting 0 items.

HTML looks like:

<SELECT style="HEIGHT: 300px" id=ListBox1 multiple size=4 name=ctl00$ContentPlaceHolderNewSys$TabContainerMain$tabPanelAdd$tabContainerInnerAdd$tabPanelAdd_1$ListBoxAll1> <OPTION value=1>param1</OPTION> <OPTION value=2>param2</OPTION> <OPTION value=3>param3</OPTION></SELECT> 

Upvotes: 0

Views: 3016

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

The function does not accept a status parameter. Modify the signature of the function to accept status as a parameter.

function LeftToRightMoveItems(status) {
...
}

There were also some issues with the code once the status was provided. Mainly there were occurrences where the varFromBox was used as a select when it actually represented an array of options.

For example, varFromBox is initially declared as an array of objects:

var varFromBox = document.getElementById("<%=ListBox1.ClientID%>").options;

And then in different places the code attempts to access the options property, as if varFromBox were a select element. In reality its actually looking for the options.options

 while (varFromBox.options.selectedIndex >= 0) {..}

Here is what I came up with to negate these issues. I removed the try/catch so any errors were more obvious. Also checkout this example: http://jsfiddle.net/7dVyq/

function LeftToRightMoveItems(status){

                if (status == "AddSetup") {
                    var varFromBox = document.getElementById("ListBox1").options;
                    var varToBox = document.getElementById("ListBox2");
                }

                alert(varFromBox.length);
                alert(varToBox.length);

                if ((varFromBox != null)) {
                    if (varFromBox.length < 1) {
                        alert('There are no items to move!');
                        return false;
                    }
                    console.log(varFromBox.selectedIndex);
                    if (varFromBox.selectedIndex == -1) // when no Item is selected the index will be -1
                    {
                        alert('Please select an item to move!');
                        return false;
                    }
                    for (var i = 0; i < varFromBox.length; i++) {
                        if (varFromBox[i].selectedIndex != -1) {
                            var newOption = new Option(); // Create a new instance of ListItem
                            newOption.text = varFromBox[i].text;
                            newOption.value = varFromBox[i].value;
                            varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
                            document.getElementById("ListBox1").remove(varFromBox[i]); //Remove the item from Source Listbox
                        }
                    }
                    return false;
                }
            }

Upvotes: 1

Related Questions