| <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ListBox Demo</title> <script type="text/javascript" language="javascript"> function AddItemInList(fromLeftToRight, isAll) { var list1 = document.getElementById('<%= ListBox1.ClientID %>'); var list2 = document.getElementById('<%= ListBox2.ClientID %>'); if(Boolean(fromLeftToRight) == true) { MoveItems(list1,list2,isAll); } else { MoveItems(list2,list1,isAll); } return false; } function MoveItems(listFrom, listTo, isAll) { var toBeRemoved = ""; if(listFrom.options.length > 0) { for (i=0; i<listFrom.length; i++) { if (listFrom.options[i].selected || (isAll == true)) { if(Exist(listTo, listFrom.options[i].value) == 0) { listTo[listTo.length] = new Option(listFrom.options[i].text, listFrom.options[i].value, true); toBeRemoved = toBeRemoved + listFrom.options[i].value + ','; } } } ClearSelection(listTo); RemoveFromList(listFrom, toBeRemoved); } else { alert('Unable to Move Items. List is Empty!'); } } function RemoveFromList(listFrom, items) { var toBeRemoved = items.split(','); for (var i=0; i < toBeRemoved.length; i++) { for (var j = 0; j < listFrom.length; j++) { if (listFrom.options[j] != null && listFrom.options[j].value == toBeRemoved[i]) { listFrom.options[j] = null; } } } } function ClearSelection(list) { list.selectedIndex = -1; } function Exist(list, value) { var flag = 0; for (var i=0; i < list.length; i++) { if (list.options[i].value == value) { flag = 1; break; } } return flag; } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td > <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Width="150px" Height="150px"> <asp:ListItem>A</asp:ListItem> <asp:ListItem>B</asp:ListItem> <asp:ListItem>C</asp:ListItem> <asp:ListItem>D</asp:ListItem> <asp:ListItem>E</asp:ListItem> </asp:ListBox> </td> <td valign="middle" align="center" style="width:100px"> <asp:Button ID="ButtonAdd" runat="server" Text=">" Width="50px" OnClientClick="return AddItemInList(true,false);"/><br /> <asp:Button ID="ButtonRemove" runat="server" Text="<" Width="50px" OnClientClick="return AddItemInList(false,false);"/> <br /> <asp:Button ID="ButtonAddAll" runat="server" Text =">>>" Width="50px" OnClientClick="return AddItemInList(true,true);"/><br /> <asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" Width="50px" OnClientClick="return AddItemInList(false,true);"/> </td> <td><asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple" Width="150px" Height="150px"></asp:ListBox></td> </tr> </table> </div> </form> </body> </html> |