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

Transfer listbox item from codebehind

16 Answers 419 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 06 Jan 2011, 10:43 PM
Is there a way to transfer items in the code behind. Specifically on a button click. I'm loading a panel with a button click and I have two list boxes. One is loaded with the item via linq. The second listbox has items that were previously selected and is loaded from a database table via linq in the code behind. This seems straight forward, but I can't seem to find an example. Using Add and Remove isn't working. I get an error on the remove, "Index was out of range. Must be non-negative and less than the size of the collection."

Major Beneftis:<br />
<telerik:RadListBox runat="server" ID="majorBenefitRadListSource" Height="150px" Width="400px"
    DataSourceID="LinqDataMajorBenefit" DataValueField="majorBenefitId" 
    DataTextField="majorBenefit"
    DataKeyField="majorBenefitId"
    AllowTransfer="true" TransferToID="majorBenefitRadListDestination" Sort="Ascending" 
    AllowTransferDuplicates="false" >
</telerik:RadListBox>
<telerik:RadListBox runat="server" ID="majorBenefitRadListDestination" Width="400px" Height="150px" />

16 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 10 Jan 2011, 02:29 PM
Hello Tim,

Thank you for contacting us.

There is a build-in functionality for transfer between listboxes.
Here you could find demo showing how to use it.


Greetings,
Dimitar Terziev
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Tim
Top achievements
Rank 1
answered on 10 Jan 2011, 05:34 PM
Thanks for the information, but the demo doesn't show how to deal with an update senerio. I'm bringing back data for a given case # and need to set up the list boxes with the values stored in the db. The source list is preloaded via linq (DataSourceId = "LinqDataSource"), if I use the add method on the destination box from the code behind it adds a new item and when you move it back to the source listbox you have two entries.
0
Dimitar Terziev
Telerik team
answered on 11 Jan 2011, 02:12 PM
Hello Tim,

Thank you for contacting us again.

What you really need is this demo here, where both listboxes are bound to databases. Please note that this is supported on limited case of scenarios. One of the necessary conditions is that both tables should have similar data items, because you could directly transfer items which are of the same kind. Second both tables should have similar primary key types and most important in your case is to set "AllowAutomaticUpdates = true" property which will insure that each time an item is transferred both data source are updated. This way there will be no duplicates to worry about.


Kind regards,
Dimitar Terziev
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Daniel Aquere
Top achievements
Rank 2
answered on 02 May 2011, 01:08 PM
Hi Everyone,

Please, I´d like some light over my problem...

I have radlistbox1 and radlistbox2:

<telerik:RadListBox ID="RadListBox1" runat="server" DataKeyField="codigo"
    DataSortField="nome" DataSourceID="SqlDataSource_empresas_0201" DataTextField="nome"
    DataValueField="codigo" Skin="Sunset" TransferToID="RadListBox_Empresa_0201_b"
    AllowTransfer="true" AllowTransferOnDoubleClick="true" EnableDragAndDrop="true"
    SelectionMode="Multiple" Culture="pt-BR" Height="200px" Width="250px" BackColor="Transparent">
</telerik:RadListBox>

<telerik:RadListBox ID="RadListBox2" runat="server" Skin="Sunset" AllowReorder="true"
    EnableDragAndDrop="true" SelectionMode="Multiple" Culture="pt-BR" Height="200px" Width="250px"
    EmptyMessage="Selecione a(s) Empresa(s)">
    <EmptyMessageTemplate>
       <img src="images\interrogacao_01.gif" alt="Nenhuma Empresa selecionada" style="vertical-align:middle;">
    </EmptyMessageTemplate>
</telerik:RadListBox>

When I click in a button I need to transfer one item from listbox1 to listbox2 by C# code. I´m using this instruction:

RadListBoxItem itemx = RadListBox1.TransferToListBox.Items[2]; (error here)
itemx.Remove();

But, appears this error message: ""Index was out of range. Must be non-negative and less than the size of the collection.". I have seven items in my listbox1.

My listbox1 have seven items.

Is it necessary after to transfer from listbox1 to listbox2 to delete this item from listbox1?

Thanks in advance, best

Daniel
0
Shinu
Top achievements
Rank 2
answered on 02 May 2011, 01:36 PM
hello Daniel,

I suggest that you use the integrated RadListBox transfer capabilities. Please, take a look at the following demo and the help article.
Transfer.
ListBox / Transfer

Thanks,
Shinu.
0
Daniel Aquere
Top achievements
Rank 2
answered on 02 May 2011, 01:54 PM
Hi Shinu,

Thanks for your quickly response.

The example and help don´t explain about transfer by code behind (C#).

I need to transfer by C# code.

Please, some idea how about I do this.

Thanks, best

Daniel
0
Dimitar Terziev
Telerik team
answered on 05 May 2011, 10:48 AM
Hi Daniel,

If you don't want to used the build in functionality for transferring, then you should drop the usage of the standard properties like TransferToID and provide your own implementation.

In general when you use the build in functionality and you have AutoPostBackOnTransfer= True then a post-back occurs and you could use the server-side events like Transferred and Transferring to access the items being transferred.

I've prepared an example page with the above mentioned events implemented.

All the best,
Dimitar Terziev
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Daniel Aquere
Top achievements
Rank 2
answered on 05 May 2011, 12:37 PM
Hi Dimitar,

Thanks for you attention.

In true, I believe that my need is more simple.

I have two listbox, the first listbox I charge with table in database, the second contain items from first, then, I need to recognise the items that exists on the second and delete from the first. I´m doing this when page is loaded.
I´m trying to use this command (without sucess):

RadListBoxItem itemx = RadListBox1.TransferToListBox.Items[2];
itemx.Remove();

Please, any tip?

Thanks, abraço

Daniel
0
Daniel Aquere
Top achievements
Rank 2
answered on 10 May 2011, 02:18 AM
Hi everyone,

Please, some light over this issue...

Thanks

Daniel
0
Shinu
Top achievements
Rank 2
answered on 10 May 2011, 12:41 PM
Hello Daniel,

Try the following approach to delete the source ListBox item.
C#:
RadListBoxItem itemx = RadListBox1.TransferToListBox.Items[2];
RadListBoxItem item = RadListBox1.FindItemByText(itemx.Text);
item.Remove();

Also check out the following help article:
Working in server-side code.

Thanks,
Shinu.
0
Daniel Aquere
Top achievements
Rank 2
answered on 11 May 2011, 04:47 AM
Hi Shinu,

I tried to implement your suggestions, but, without sucess.
In this line:
RadListBoxItem itemx = RadListBox1.TransferToListBox.Items[2];
I receive this error message:
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
My listbox has seven items. Item 2 exists!

Another point is... I tried to delete items, thats ok, but, what is the command (C#) to force listbox update on the screen? Because when I delete items, the screen don´t update.

Thanks for your help.

Best

Daniel
0
Shinu
Top achievements
Rank 2
answered on 11 May 2011, 08:58 AM
Hello Daniel,

The RadListBox1.TransferToListBox returns the RadListBox which the current list box is configured to transfer to via the TransferToID property.
That is you will be getting the second ListBox. In your scenario, during pageLoad the second ListBox doesn't contain the values always. So in page load check for the Item count of the second ListBox and do the same.

Hope it helps.

Thanks,
Shinu.
0
Daniel Aquere
Top achievements
Rank 2
answered on 18 May 2011, 04:49 AM
Hi Shinu,

My RadListBox1 is charged with sqldatasource on pageload and contain 7 (seven) elements.

I see these element when I debug the application.

Please, any tip?

Thanks, best

Daniel
0
Daniel Aquere
Top achievements
Rank 2
answered on 20 May 2011, 07:56 PM
Hi Shinu,

Please, any news about this issue...

Thanks,

Daniel
0
Dimitar Terziev
Telerik team
answered on 23 May 2011, 09:19 AM
Hello Daniel,

Have you tried referencing the second listbox through its ID, not using the RadListBox1.TransferToListBox?

On the declaration you have provided it seems to me that you have set different ID in the TranserToID, than the ID of the second RadListBox:
<telerik:RadListBox ID="RadListBox1" runat="server" DataKeyField="codigo"
    DataSortField="nome" DataSourceID="SqlDataSource_empresas_0201" DataTextField="nome"
    DataValueField="codigo" Skin="Sunset" TransferToID="RadListBox_Empresa_0201_b"
    AllowTransfer="true" AllowTransferOnDoubleClick="true" EnableDragAndDrop="true"
    SelectionMode="Multiple" Culture="pt-BR" Height="200px" Width="250px" BackColor="Transparent">
</telerik:RadListBox>
 
<telerik:RadListBox ID="RadListBox2" runat="server" Skin="Sunset" AllowReorder="true"
    EnableDragAndDrop="true" SelectionMode="Multiple" Culture="pt-BR" Height="200px" Width="250px"
    EmptyMessage="Selecione a(s) Empresa(s)">
    <EmptyMessageTemplate>
       <img src="images\interrogacao_01.gif" alt="Nenhuma Empresa selecionada" style="vertical-align:middle;">
    </EmptyMessageTemplate>
</telerik:RadListBox>

Please note that when you have data source defined in the markup and the control is binded automatically, its items are added in Page_Prerender, so in Page_Load the items count of the first listbox will be 0. In case you want to force binding of the RadListBox you should call DataBind method and then you will be able to reference its items.

Kind regards,
Dimitar Terziev
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Daniel Aquere
Top achievements
Rank 2
answered on 29 May 2011, 11:33 PM
Hi Dimitar,

Thanks for your support, problem solved with onTransferred event.

Best,

Daniel
Tags
ListBox
Asked by
Tim
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Tim
Top achievements
Rank 1
Daniel Aquere
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or