开发者

Move item from listbox1 to listbox2 in web application

开发者 https://www.devze.com 2023-03-23 13:43 出处:网络
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 department

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 (S开发者_如何学运维tring.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.


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


} 


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号