Move item from listbox1 to listbox2 in web application

1.2k Views Asked by At

I have 2 listboxes. One contains all the department which are unassigned and other listbox contains all the department which is assigned to a particular person. Now I want to add and delete departments using insert and delete query. The query will be performed on 1 table only ie Assigned Department(listbox2) and by clicking the SAVE button. I have done the insert part but unable to handle the delete part. I have seen few examples but they dont have DB event in them.

protected void Save_Click(object sender, EventArgs e)
{
    DataTable dt = GetData2();
    bool found = false;

    foreach (RadListBoxItem item in RadListBox2.Items)
    {

        found = false;
        foreach (DataRow dr in dt.Rows)
        {
            if (String.Compare(item.Value, dr["DeptID"].ToString()) == 0)
            {
                found = true; 
                label1.Text = "Add a New Group to the ListBox";
            }

        }
        if (found == false)
        {
            Item(item.Text, item.Value);
        }

This is what I am trying to do. I want to handle insert and delete event on Save button.

2

There are 2 best solutions below

0
On BEST ANSWER

I figured out the solution so I am posting it in here.

    foreach (DataRow dr in dt.Rows)
    {
        found = false;
        foreach (RadListBoxItem item in RadListBox2.Items)
        {
            if (String.Compare(item.Value, dr["DeptID"].ToString()) == 0)
            {
                found = true;
            }
        }

            if (found == false)
            {
                 //delete here
            }
        }


} 
0
On

I use the RadListBox's Transferred event. When the Transferred event fires Items look at the destination listbox's id and determine if the items were moved to the unassigned or assigned ListBox. Once you know the destination you can execute a query to add or remove rows from your database.

In any case, your Query will be dependent on your schema. In my situation I had a lookup table that determined if the item was attached.

rlistAvailable_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
{
        if (e.DestinationListBox.ID == "rlistAttached") 
        {
            foreach (Telerik.Web.UI.RadListBoxItem li in e.Items) 
                {
            //do insert query using li.Value
                }
        } 
        else 
        {
            foreach (Telerik.Web.UI.RadListBoxItem li in e.Items) 
                    {
            //do delete query using li.Value
                }
         }
}