Transfer listbox item from codebehind

17 posts, 0 answers
  1. Tim
    Tim avatar
    39 posts
    Member since:
    Oct 2010

    Posted 06 Jan 2011 Link to this post

    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" />
  2. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 10 Jan 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tim
    Tim avatar
    39 posts
    Member since:
    Oct 2010

    Posted 10 Jan 2011 Link to this post

    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.
  5. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 11 Jan 2011 Link to this post

    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.
  6. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 02 May 2011 Link to this post

    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
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 May 2011 Link to this post

    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.
  8. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 02 May 2011 Link to this post

    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
  9. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 05 May 2011 Link to this post

    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.

  10. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 05 May 2011 Link to this post

    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
  11. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 09 May 2011 Link to this post

    Hi everyone,

    Please, some light over this issue...

    Thanks

    Daniel
  12. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 May 2011 Link to this post

    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.
  13. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 10 May 2011 Link to this post

    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
  14. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 11 May 2011 Link to this post

    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.
  15. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 17 May 2011 Link to this post

    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
  16. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 20 May 2011 Link to this post

    Hi Shinu,

    Please, any news about this issue...

    Thanks,

    Daniel
  17. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 23 May 2011 Link to this post

    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.

  18. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 29 May 2011 Link to this post

    Hi Dimitar,

    Thanks for your support, problem solved with onTransferred event.

    Best,

    Daniel
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017