This is a migrated thread and some comments may be shown as answers.

How to get data changes from transferred items

3 Answers 184 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Daniel Sprague
Top achievements
Rank 1
Daniel Sprague asked on 22 Mar 2013, 12:14 PM

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;
    }
}

3 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 27 Mar 2013, 11:20 AM
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.
0
Daniel Sprague
Top achievements
Rank 1
answered on 27 Mar 2013, 11:35 AM
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.
0
Nencho
Telerik team
answered on 01 Apr 2013, 10:12 AM
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.
Tags
ListBox
Asked by
Daniel Sprague
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Daniel Sprague
Top achievements
Rank 1
Share this question
or