Wrong items order when transferring in Rad ListBox control

4 posts, 1 answers
  1. Triet Pham
    Triet Pham avatar
    4 posts
    Member since:
    Aug 2009

    Posted 24 Aug 2009 Link to this post

    Hi Supporters!

    I've used 2 listbox controls ( i called available listbox and selected listbox) in a page. The available listbox is showing all available items which user can select. Other listbox is showing all user selected items. User can select an item or select many items in available listbox, and drag, drop those into selected listbox. This page seems like the demo ListBox page in Telerik website.
    Both are support autopostback, drag & drop, multi-select... except reorder.

    However, i met a strange behaviour when i drag some items in available listbox and drop to selected one. The order of dragging items are reversed when dropped into target listbox. For example:

    Available listbox:
    1. Item1.
    2. Item2.
    3. Item3.
    4. Item4.

    Selected listbox is empty.

    I choose item1,item2,item3 in available listbox and drop into selected listbox. The order of those items is reversed. So the selected listbox has items in order of item3, item2, item1. I expect the selected listbox should keep the same order of source items.

    I saw the same result in demo ListBox in Telerik demo site. Is it a bug of ListBox control? or do you have an alternative solution to work around?

    Thanks for your attention.
    Triet Pham.
  2. Answer
    Simon
    Admin
    Simon avatar
    2281 posts

    Posted 26 Aug 2009 Link to this post

    Hi Triet Pham,

    We have recently detected this issue and will address it in the upcoming weeks (it will not be fixed in the Service Pack).

    You could resolve it temporary at your side by handling the server-side Transferred event of the source ListBox as shown below:

    [C#]
        void RadListBox1_Transferred(object sender, RadListBoxTransferredEventArgs e) 
        { 
            int transferredItemsCount = e.Items.Count; 
            int destItemsCount = RadListBox1.TransferToListBox.Items.Count; 
            int insertPosition = destItemsCount - transferredItemsCount; 
     
            for (int i = 0; i < transferredItemsCount - 1; i++) 
            { 
                RadListBoxItem item = RadListBox1.TransferToListBox.Items[insertPosition + 1 + i]; 
                item.Remove(); 
                RadListBox1.TransferToListBox.Items.Insert(insertPosition, item); 
            } 
        } 

    [VB]
    Private Sub RadListBox1_Transferred(ByVal sender As ObjectByVal e As RadListBoxTransferredEventArgs) 
        Dim transferredItemsCount As Integer = e.Items.Count 
        Dim destItemsCount As Integer = RadListBox1.TransferToListBox.Items.Count 
        Dim insertPosition As Integer = destItemsCount - transferredItemsCount 
         
        For i As Integer = 0 To transferredItemsCount - 2 
            Dim item As RadListBoxItem = RadListBox1.TransferToListBox.Items(insertPosition + 1 + i) 
            item.Remove() 
            RadListBox1.TransferToListBox.Items.Insert(insertPosition, item) 
        Next 
    End Sub 
     

    Kind regards,
    Simon
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Triet Pham
    Triet Pham avatar
    4 posts
    Member since:
    Aug 2009

    Posted 27 Aug 2009 Link to this post

    Yep, it works now with your help.
    Thank you so much, Simon.

  5. Jeff Berry
    Jeff Berry avatar
    13 posts
    Member since:
    Jun 2009

    Posted 16 Oct 2009 Link to this post

    Well, this solution sort of works (if you only want to transfer in one direction).  Transferring back to the source listbox bombs the above code on an index out of range.  Here is corrected C# code to handle transfer in both directions:

        protected void rlbAvailableFields_Transferred( object sender, RadListBoxTransferredEventArgs e )  
        {  
            int transferredItemsCount = e.Items.Count;  
            int destItemsCount = 0;  
            int insertPosition  = 0;  
     
            if ( e.DestinationListBox == rlbRequestedFields )  
            {  
                destItemsCount = rlbAvailableFields.TransferToListBox.Items.Count;  
                insertPosition = destItemsCount - transferredItemsCount;  
                for ( int i = 0; i < transferredItemsCount - 1; i++ )  
                {  
                    RadListBoxItem item = rlbAvailableFields.TransferToListBox.Items[insertPosition + 1 + i];  
                    item.Remove( );  
                    rlbAvailableFields.TransferToListBox.Items.Insert( insertPosition, item );  
                }  
            }  
            else  
            {  
                destItemsCount = rlbAvailableFields.Items.Count;  
                insertPosition = destItemsCount - transferredItemsCount;  
                for ( int i = 0; i < transferredItemsCount - 1; i++ )  
                {  
                    RadListBoxItem item = rlbAvailableFields.Items[insertPosition + 1 + i];  
                    item.Remove( );  
                    rlbAvailableFields.Items.Insert( insertPosition, item );  
                }  
            }  
        } 

    rlbAvailableFields is the source listbox (ie the one with AllowTransfer="true" and TransferToID="rlbRequestedFields"). rlbRequestedFeilds is the destination listbox.

    - Jeff
Back to Top