Statistics
Visits: 105081
Page Visits: 139143
Active visitors (monthly): 5364
Feed Count: 45
Most visited page: Adding value as a technical analyst
Archive : Jul 2011

Move items from one listbox to another using jQuery

I have two listboxes - list1, list2. There are four buttons. Add button to move selected items from one listbox to another. AddAll button to move all items from one listbox to another. Remove and RemoveAll buttons do the reverse operation. The code below shows how this can be done using jQuery.

HTML


    <div>
        <asp:ListBox ID="list1" runat="server">
            <asp:ListItem Text="One"></asp:ListItem>
            <asp:ListItem Text="Two"></asp:ListItem>
            <asp:ListItem Text="Three"></asp:ListItem>
        </asp:ListBox>
        <asp:Button ID="btnAdd" runat="server" Text=">" />
        <asp:Button ID="btnAddAll" runat="server" Text=">>" />
        <asp:Button ID="btnRemove" runat="server" Text="<"/>
        <asp:Button ID="btnRemoveAll" runat="server" Text="<<" />
        <asp:ListBox ID="list2" runat="server"></asp:ListBox>
    </div>

Javascript


<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(
        function () {
            $('#btnAdd').click(
                function (e) {
                    $('#list1 > option:selected').appendTo('#list2');
                    e.preventDefault();
                });

                $('#btnAddAll').click(
                function (e) {
                    $('#list1 > option').appendTo('#list2');
                    e.preventDefault();
                });

                $('#btnRemove').click(
                function (e) {
                    $('#list2 > option:selected').appendTo('#list1');
                    e.preventDefault();
                });

                $('#btnRemoveAll').click(
                function (e) {
                    $('#list2 > option').appendTo('#list1');
                    e.preventDefault();
                });
 
        });
</script>

The selector for selecting items in a listbox is $('list1 > option:selected'). AppendTo function moves item from one element to another. e.preventDefault prevents event bubbling so that the page is not posted back.

Permalink | 5 Comments | Leave your comment
 
Commented by Vijay at 08-Sep-2011 10:13 PM

@Manivannan, I want to investigate this a little bit more. There is also another requirement where moving a item from one listbox to another should not affect the sort order.

I intended this post to be a simple post which shows what can be done using jQuery. Since, there is some interest in this post, I will spend my time and make it better.

Commented by ManivannaN at 08-Sep-2011 11:19 AM

Hi Vijay..

In the code behind,Wen v trying to access the listbox item countin the target source.. it still showing zero..?? Do u hav any idea abt this issue..??

Commented by Arunshyam at 07-Sep-2011 06:58 PM

Really nice code...

Commented by Vijay at 15-Aug-2011 09:44 AM

There is no sort function readily available for listbox. One way to do this is to use the each() function for the listbox items. I will try to come up with another post on this when I find a neat solution.

Commented by vhinz at 11-Aug-2011 03:40 PM

Your code is good.. I love it. Anyway can you help me with this. Can we sort the listbox after moving it..

Little things about the Page Lifecycle

PreInit: If you want to change the masterpage programatically, this is the event handler where it should be done: http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx. Trying to change the MasterPage in any other event will cause an InvalidOperationException. This is because the masterpage rearranges the hierarchy of controls.

This is also the event handler where themes should be set programatically. If you try to change themes elsewhere, this is what you get: Invalid operation exception -
The 'Theme' property can only be set in or before the 'Page_PreInit' event.  http://msdn.microsoft.com/en-us/library/tx35bd89.aspx

Dynamic controls have to be recreated each time in PreInit. If dynamic controls are not recreated (on each postback), they do not appear in the page. If dynamic controls are created after PreInit method, the themes are not properly set.

Init: You can init control properties here. This is generally not used at all. This is because of the next event - LoadViewState

LoadViewState: LoadViewState loads control properties from the ViewState. This usually works in the case of postbacks.

Load: Do databinding of controls here manually. Initialize controls (for first time). Most initialization is done for the first time (IsPostback=false)

UserControl Load: Each UserControls Load is called. Here user controls are initialized.

LoadComplete: If you want to make some initializations based on UserControl's state, this is where to do it. Very rarely used. This is because UserControls initialize from ViewState. This is usually used when EnableViewState=false

Validation: If any validator controls are there in the page, this event is fired. After this event, Page.IsValid property is set. This is fired for postbacks

RaisePostBackEvent: This is the postback event handler where you write your code. Before writing any code, check if the page is valid. (IsValid=true)

PreRender: Used to emit custom scripts.

Automatic DataBinding: If you are using SqlDataSource, XmlDataSource etc, this is where automatic data binding happens. Automatic DataBinding happens only when IsPostBack=false (for the first time). In Postbacks, the ViewState loads the data.

SaveViewState: Saves the control properties into ViewState (which goes into the Response)

Render: Renders the control into the Response stream as HTML + Javascript.

Unload: Page and all its objects are released from memory.

 

Permalink | No Comments | Leave your comment