This is a migrated thread and some comments may be shown as answers.

Wrong items order when transferring in Rad ListBox control

3 Answers 228 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Triet Pham
Top achievements
Rank 1
Triet Pham asked on 24 Aug 2009, 09:19 AM
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.

3 Answers, 1 is accepted

Sort by
0
Accepted
Simon
Telerik team
answered on 26 Aug 2009, 02:47 PM
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.
0
Triet Pham
Top achievements
Rank 1
answered on 27 Aug 2009, 03:56 PM
Yep, it works now with your help.
Thank you so much, Simon.

0
Jeff Berry
Top achievements
Rank 2
answered on 16 Oct 2009, 12:52 PM
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
Tags
ListBox
Asked by
Triet Pham
Top achievements
Rank 1
Answers by
Simon
Telerik team
Triet Pham
Top achievements
Rank 1
Jeff Berry
Top achievements
Rank 2
Share this question
or