How to get data changes from transferred items

4 posts, 0 answers
  1. Daniel Sprague
    Daniel Sprague avatar
    17 posts
    Member since:
    Feb 2010

    Posted 22 Mar 2013 Link to this post

    Hi everyone,
    Any and all assistance here is greatly appreciated!

    I'm trying to implement a Listbox Transfer scenario - allowing typical moving items from one list box to another.
    The transferring of items back and forth is working fine.
    The part that isn't working is getting changes to update a SQL database.
    But once something is moved, the underlying dataTable doesn't have any changes.

    What am I missing in order to acknowledge changes in the Selected dataTable?
    Thank you in advance.

    Here's the listboxes defined:

    <telerik:RadListBox ID="RadListBox1" CssClass="RadListBoxAvailable" runat="server" SelectionMode="Multiple" AllowTransfer="True" TransferToID="RadListBox2" AllowTransferOnDoubleClick="True" EnableDragAndDrop="True" OnTransferred="RadListBox1_Transferred" AutoPostBackOnTransfer="True" Sort="Ascending"></telerik:RadListBox>
                             
    <telerik:RadListBox ID="RadListBox2" CssClass="RadListBoxSelected" runat="server" SelectionMode="Multiple" AllowTransferOnDoubleClick="True"  EnableDragAndDrop="True" AutoPostBackOnTransfer="True" Sort="Ascending"></telerik:RadListBox>

    And here's the c#:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using Telerik.Web.UI;
     
    public partial class testX : System.Web.UI.Page
    {
        public static DataTable dtSelected;
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAvailable();
                LoadSelected();
            }
        }
     
        private void LoadAvailable()
        {
            string query = "select personId, person from peopleList";
            RadListBox1.DataSource = GetDataTable(query);
            RadListBox1.DataSortField = "person";
            RadListBox1.DataTextField = "person";
            RadListBox1.DataValueField = "personId";
            RadListBox1.DataBind();
        }
     
        private void LoadSelected()
        {
            string query = "select personId, person from peopleChosen";
            dtSelected = GetDataTable(query);
            RadListBox2.DataSource = dtSelected;
            RadListBox2.DataTextField = "person";
            RadListBox2.DataSortField = "person";
            RadListBox2.DataValueField = "personId";
            RadListBox2.DataBind();
        }
     
        private DataTable GetDataTable(string query)
        {
            String ConnString = ConfigurationManager.ConnectionStrings["wareConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(query, conn);
     
            DataTable myDataTable = new DataTable();
     
            conn.Open();
            try         {adapter.Fill(myDataTable);}
            finally        {conn.Close();}
     
            return myDataTable;
        }
     
        protected void RadListBox1_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
        {
            if (DataTableHasChanges(dtSelected))
            {
                LabelEditStatus.Text = "Changes not Saved";
            }
            else
            {
                LabelEditStatus.Text = "";
            }
        }
     
        public bool DataTableHasChanges(DataTable dataTable)
        {
            return (dataTable != null) ? dataTable.GetChanges() != null : false;
        }
    }
  2. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 27 Mar 2013 Link to this post

    Hello Daniel,

    I can suggest you to use the approach, demonstrated in our online demon here. To allow the functionality demonstrated in the demo, you should set the AllowAutomaticUpdates property to true and set the autopostback property to true for the desired operations(AutoPostBackOnReorder / AutoPostBackOnTransfer/ AutoPostBackOnDelete).


    All the best,
    Nencho
    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
  4. Daniel Sprague
    Daniel Sprague avatar
    17 posts
    Member since:
    Feb 2010

    Posted 27 Mar 2013 Link to this post

    Thanks Nencho for the reply.
    However, that Automatic functionality (immediate updates) is not what is desired.

    What I need to implement is a user being able to make changes to the page, then click a Save button.
    I have other controls on this page, and it could be quite confusing for users and lead to many headaches if the listBox changes were automatcally updated, but nothng else.

    This is why I need to determine if changes were made in the listBox on the right. 
    Not just if items were moved from one box to the other, (a user could move the same item from the left to right, then back, and that doesn't need to be known), but if the contents of the Selected box are different than when the page was initially rendered. 

    Please inform me to be able to determine if changes exist in the lilstBox contents.

    Thank you in advance.
  5. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 01 Apr 2013 Link to this post

    Hello Daniel,

    As I can see, you are trying to persist the initial state of the DataSource in the dtSelected DataTable variable. Please keep in mind, that the changes in the RadListBoxes, are not reflecting the state of the datatable variable. Therefor, you could not rely on the GetChanges() method, in order to determine if any changes are made on the state of the controls.

    I can suggest you to perform the database operation, in the OnTransferred event handler, each time an action (transferring ) is performed. Otherwise, you could store the Items in a List<RadListBoxItem> for the current and the initial state of the control and compare them in the following manner :
    protected void RadListBox1_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
        {
            var rowsCount = dtSelected.Rows.Count;
            List<RadListBoxItem> newList = new List<RadListBoxItem>();
            List<RadListBoxItem> dataSourceList = new List<RadListBoxItem>();
     
            for (int i = 0; i < rowsCount; i++)
            {
                newList.Add(new RadListBoxItem(dtSelected.Rows[0].ItemArray[1].ToString(), dtSelected.Rows[0].ItemArray[0].ToString()));
            }
     
            foreach (RadListBoxItem item in RadListBox2.Items)
            {
                dataSourceList.Add(item);
            }
     
            if (dataSourceList != newList)
            {
                LabelEditStatus.Text = "Changes not Saved";
            }
            else
            {
                LabelEditStatus.Text = "";
            }
        }




    All the best,
    Nencho
    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.
Back to Top