New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Transfer Items to ListBox Loaded on Demand
In order to transfer items to a RadListBox that is using the Load on Demand mechanism, you need to set the AutoPostBackOnTransfer property of the source RadListBox to "true" and in its OnTransferred server-side event handler insert the items in the destination RadListBox's data source.
- Controls declaration:
ASPNET
<telerik:RadListBox RenderMode="Lightweight" runat="server" ID="RadListBox1" Height="200px"
TransferToID="RadListBox2" AllowTransfer="true" AutoPostBackOnTransfer="true"
OnTransferred="RadListBox1_Transferred">
<Items>
<telerik:RadListBoxItem Text="New Product1" />
<telerik:RadListBoxItem Text="New Product2" />
<telerik:RadListBoxItem Text="New Product3" />
</Items>
</telerik:RadListBox>
<telerik:RadListBox RenderMode="Lightweight" runat="server" ID="RadListBox2" Height="200px"
EnableLoadOnDemand="true" DataSourceID="SqlDataSource1"
DataTextField="ProductName" DataValueField="ProductID" DataKeyField="ProductID">
</telerik:RadListBox>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]"></asp:SqlDataSource>
- The OnTransferred handler:
C#
protected void RadListBox1_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
{
foreach (RadListBoxItem item in e.Items)
{
SqlConnection destinationCon = new SqlConnection("Data Source=.\\;AttachDbFilename=|DataDirectory|\\Northwind.mdf;Integrated Security=True;User Instance=True;");
destinationCon.Open();
SqlCommand insertCommand = destinationCon.CreateCommand();
insertCommand.CommandText = "INSERT INTO Products (ProductName) VALUES (@ProductName)";
insertCommand.Parameters.AddWithValue("@ProductName", item.Text);
insertCommand.ExecuteNonQuery();
}
}