New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Duplicate Items after Transfer and AJAX Request
Problematic situation (Example 1):
- Move an item from a listbox to another listbox
- Make an AJAX call that does not include the listboxes
- Make a postback
Issue: After the postback, the transferred item is duplicated and you may get a server error.
Solution: Make the listboxes part of the response (Example 2), or set their AutoPostBackOnTransfer
property to true
(Example 3).
Description and origin of the problem:
- An item is transferred to the second listbox.
- Both listboxes update their ClientState field with information of this change.
- An AJAX request is made, this information is POSTed to the server.
- The listboxes use this information and update their state on the server.
- They are not part of the response, so their ClientState fields are not updated to reflect their server-side state, so they still contain information that an item must be transferred.
- Another postback is made, which POSTs information that the transfer must happen.
- Since the transfer has already happened, you may get an error. At the very least, the item would be duplicated.
- Then, after a full postback the ClientState of the listboxes is now updated to reflect their server state.
Example 1: Problematic scenario
ASP.NET
<table>
<tr>
<td>Step 1. Move one or more items from left box to right box.</td>
</tr>
<tr>
<td>
<telerik:RadListBox runat="server" ID="RadListBox1" Height="200px" Width="300px"
AllowTransfer="True" TransferToID="RadListBox2" />
</td>
<td>
<telerik:RadListBox runat="server" ID="RadListBox2" Height="200px" Width="235px" />
</td>
</tr>
</table>
<br /><br />
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<telerik:RadButton runat="server" ID="RadButton1" Text="Step 2. Make AJAX Call" OnClick="RadButton1_Click" />
<asp:Literal ID="Literal1" runat="server" />
</telerik:RadAjaxPanel>
<br /><br />
<telerik:RadButton runat="server" ID="RadButton2" Text="Step 3. Show Selected" OnClick="RadButton2_Click" />
<br />
<asp:Literal ID="Literial2" runat="server" />
Example 2: Include the listboxes in the response
ASP.NET
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<table>
<tr>
<td>Step 1. Move one or more items from left box to right box.</td>
</tr>
<tr>
<td>
<telerik:RadListBox runat="server" ID="RadListBox1" Height="200px" Width="300px"
AllowTransfer="True" TransferToID="RadListBox2" />
</td>
<td>
<telerik:RadListBox runat="server" ID="RadListBox2" Height="200px" Width="235px" />
</td>
</tr>
</table>
<br /><br />
<telerik:RadButton runat="server" ID="RadButton1" Text="Step 2. Make AJAX Call" OnClick="RadButton1_Click" />
<asp:Literal ID="Literal1" runat="server" />
</telerik:RadAjaxPanel>
<br /><br />
<telerik:RadButton runat="server" ID="RadButton2" Text="Step 3. Show Selected" OnClick="RadButton2_Click" />
<br />
<asp:Literal ID="Literial2" runat="server" />
Example 3: Make the listboxes postback automatically
ASP.NET
<table>
<tr>
<td>Step 1. Move one or more items from left box to right box.</td>
</tr>
<tr>
<td>
<telerik:RadListBox runat="server" ID="RadListBox1" Height="200px" Width="300px"
AllowTransfer="True" TransferToID="RadListBox2" AutoPostBackOnTransfer="true" />
</td>
<td>
<telerik:RadListBox runat="server" ID="RadListBox2" Height="200px" Width="235px" AutoPostBackOnTransfer="true" />
</td>
</tr>
</table>
<br /><br />
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<telerik:RadButton runat="server" ID="RadButton1" Text="Step 2. Make AJAX Call" OnClick="RadButton1_Click" />
<asp:Literal ID="Literal1" runat="server" />
</telerik:RadAjaxPanel>
<br /><br />
<telerik:RadButton runat="server" ID="RadButton2" Text="Step 3. Show Selected" OnClick="RadButton2_Click" />
<br />
<asp:Literal ID="Literial2" runat="server" />