Transferring Item Template Problem

2 posts, 1 answers
  1. david
    david avatar
    1 posts
    Member since:
    Aug 2011

    Posted 28 Feb 2012 Link to this post

    Hi all,

    I am having issues transfering data from one list box to another. I am using Item Templates so I know that I need to use "AutoPostBackOnTransfer" and I am handerling the listbox_transferred event. However, my data still does not display in the second list box. Can anyone spot the bit I am missing or doing incorrectly.

    default.aspx
    <%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestingListBoxTemplates._Default" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title>Testing List Boxes</title>
        <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <Scripts>
                <%--Needed for JavaScript IntelliSense in VS2010--%>
                <%--For VS2008 replace RadScriptManager with ScriptManager--%>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
            </Scripts>
        </telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
        <div>
     
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" height="800px" width="1242px">
                <telerik:RadComboBox ID="rcbContactName"  runat="server" EmptyMessage="Enter a contact name" HighlightTemplatedItems="true" EnableLoadOnDemand="true" AutoPostBack="true" />
     
                <telerik:RadListBox ID="RadListBox1" runat="server" AllowTransfer="True" height="600px" Width="400px" TransferToID="RadListBox2" AutoPostBackOnTransfer="true" OnTransferred="RadListBox1_Transferred">
                    <ItemTemplate>
                       <table width="300px">
                            <tr>
                            <td width="100px"><span><%# DataBinder.Eval(Container.DataItem, "contactID")%></span></td>
                            <td colspan="3"><span><%# DataBinder.Eval(Container.DataItem, "FirstName")%> <%# DataBinder.Eval(Container.DataItem, "Surname")%></span></td >
                            </tr>
                            <tr><td><span>Company Name:</span></td><td><span><%# DataBinder.Eval(Container.DataItem, "CompanyName")%></span></td>
                            <td><span>Industries:</span></td><td><span><%# DataBinder.Eval(Container.DataItem, "Industries")%></span></td>
                            </tr>
                            <tr><td><span>Email Address:</span></td><td><span><a href="mailto:<%# DataBinder.Eval(Container.DataItem, "Email")%>"><%# DataBinder.Eval(Container.DataItem, "Email")%></a></span></td>
                            <td><span>Interests:</span></td><td><span><%# DataBinder.Eval(Container.DataItem, "Interests")%></span></td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:RadListBox>
                <telerik:RadListBox ID="RadListBox2" runat="server" Height = "600px" Width="400px">
                    <ItemTemplate>
                    <span><%# DataBinder.Eval(Container.DataItem, "contactID")%> - 
                    <%# DataBinder.Eval(Container.DataItem, "FirstName")%> 
                    <%# DataBinder.Eval(Container.DataItem, "Surname")%></span>
                    </ItemTemplate>
                </telerik:RadListBox>
            </telerik:RadAjaxPanel>
        </div>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" Runat="server"
            Skin="Default">
        </telerik:RadAjaxLoadingPanel>
        </form>
    </body>
    </html>

    default.aspx.vb
    Imports Telerik.Web.UI
    Imports System.Data.SqlClient
     
    Partial Class _Default
        Inherits System.Web.UI.Page
     
        Public sql_Connection As String = "Data Source=TestServer;Initial Catalog=Contacts;User ID=myself;password=password;Connect Timeout=200"
     
        Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                getLBData()
                RadListBox1.DataBind()
                RadListBox2.DataBind()
            End If
        End Sub
     
        Private Sub rcbContactName_ItemsRequested(sender As Object, e As Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs) Handles rcbContactName.ItemsRequested
            If e.Text.Length > 2 Then
                getNames(e.Text)
            End If
        End Sub
     
        Private Sub getNames(ByVal fragment As String)
            Dim cnSQL As New SqlConnection(sql_Connection)
            Dim cmdSQL As New SqlCommand
            cmdSQL.Connection = cnSQL
            cmdSQL.CommandType = Data.CommandType.StoredProcedure
            cmdSQL.CommandText = "usp_ContactSearch_prefix"
            cmdSQL.Parameters.AddWithValue("@Contact", fragment)
                'If Not String.IsNullOrEmpty(rcbPartialAddress.Text) Then cmdSQL.Parameters.AddWithValue("@Address", rcbPartialAddress.Text)
                'If Not String.IsNullOrEmpty(rcbEmailAddress.Text) Then cmdSQL.Parameters.AddWithValue("@Email", rcbEmailAddress.Text)
     
                Try
                    Dim daSQL As New SqlClient.SqlDataAdapter(cmdSQL)
                    Dim dataTable As New DataTable()
                    daSQL.Fill(dataTable)
     
                    For Each dataRow As DataRow In dataTable.Rows
     
                        rcbContactName.Items.Add(New RadComboBoxItem(DirectCast(dataRow("contactName"), String), dataRow("contactid").ToString()))
     
                    Next
     
                    rcbContactName.DataBind()
     
                Catch
                Finally
                    If cnSQL.State <> Data.ConnectionState.Closed Then
                        cnSQL.Close()
                    End If
                End Try
        End Sub
     
        Private Sub rcbContactName_SelectedIndexChanged(sender As Object, e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles rcbContactName.SelectedIndexChanged
            RadListBox1.DataSource = getLBData()
            RadListBox1.DataBind()
        End Sub
     
        Public Sub RadListBox1_Transferred(sender As Object, e As Telerik.Web.UI.RadListBoxTransferredEventArgs) Handles RadListBox1.Transferred
            For Each item As RadListBoxItem In e.Items
                item.DataBind()
            Next
        End Sub
     
        Private Function getLBData()
            Dim cnSQL As New SqlConnection(sql_Connection)
            Dim cmdSQL As New SqlCommand
            cmdSQL.Connection = cnSQL
            cmdSQL.CommandType = Data.CommandType.StoredProcedure
            cmdSQL.CommandText = "usp_SearchLyrisContacts"
            cmdSQL.Parameters.Add("@ContactName", DbType.String).Value = rcbContactName.Text
     
            Dim dataTable As New DataTable()
            Try
                Dim daSQL As New SqlClient.SqlDataAdapter(cmdSQL)
     
                daSQL.Fill(dataTable)
     
            Catch
            Finally
                If cnSQL.State <> Data.ConnectionState.Closed Then
                    cnSQL.Close()
                End If
            End Try
            Return dataTable
        End Function
     
    End Class
     
  2. Answer
    Bozhidar
    Admin
    Bozhidar avatar
    1101 posts

    Posted 02 Mar 2012 Link to this post

    Hello David,

    In the example you sent, you are binding directly to the DataItem property of the item. This property is useful when the ListBox is bound to a datasource. However, when you transfer the item to the second ListBox, since it is not bound, this property is null.

    If you want to bind to properties from the DataItem, you should use the ItemDataBound event to map them to properties of the item itself. The best way to store arbitrary properties is to use the item's AttributeCollection. You can read more on how to achieve that in the following help article:
    http://www.telerik.com/help/aspnet-ajax/listbox-binding-to-aspnet-datasource-components.html 
     
    Regards,
    Bozhidar
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top