Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I want to copy all selected listbox items from one list box to another in client side.how can do this?
regards,.
shefeek
Posted
Comments
Prasad_Kulkarni 12-Mar-12 5:23am    
Have you tried anything so far? Post your code so we can help you better..
Use this code:
myListBox x = new myListBox();
x.Items.Add("a");
x.Items.Add("b");
x.Items.Add("c");
myListBox y = x.Clone() as myListBox;
for (int i = 0; i < x.Items.Count; i++)
{
MessageBox.Show(y.Items[i].ToString());
}

1 solution

HI all,
I have got the answer.
XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <script>
    function MoveItem(ctrlSource, ctrlTarget) {
        var Source = document.getElementById(ctrlSource);
        var Target = document.getElementById(ctrlTarget);
        if ((Source != null) && (Target != null)) {
            while ( Source.options.selectedIndex >= 0 ) {
                var newOption = new Option(); // Create a new instance of ListItem
                newOption.text = Source.options[Source.options.selectedIndex].text;
                newOption.value = Source.options[Source.options.selectedIndex].value;

                Target.options[Target.length] = newOption; //Append the item in Target
                Source.remove(Source.options.selectedIndex);  //Remove the item from Source
            }
        }
    }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>


<table height="150" width="300">
    <tr>
        <td>
            <asp:ListBox id="ListBox1" runat="server" Height="111px" SelectionMode="Multiple">
                <asp:ListItem Value="1">One</asp:ListItem>
                <asp:ListItem Value="2">Two</asp:ListItem>
                <asp:ListItem Value="3">Three</asp:ListItem>
            </asp:ListBox>
        </td>
        <td>
            <p>
                <input onclick="Javascript:MoveItem('ListBox1', 'ListBox2');" type="button" value="->" />
            </p>
            <p>
                <input onclick="Javascript:MoveItem('ListBox2', 'ListBox1');" type="button" value="<-" />
            </p>

        </td>
        <td>
            <asp:ListBox id="ListBox2" runat="server" Height="111px" SelectionMode="Multiple">
                <asp:ListItem Value="8">Eight</asp:ListItem>
                <asp:ListItem Value="9">Nine</asp:ListItem>
                <asp:ListItem Value="10">Ten</asp:ListItem>
            </asp:ListBox>
        </td>
    </tr>
</table>


    </div>
    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900