Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am having 2 List boxes where one list box will be displayed based on the drop down list selected value and if i select an item from first list box and click on that item will be placed in to the second list box and item and will be saved in the database and the item that added will be removed from the first list box this works fine. If i select another item and saving only the first item in the second list box was saving.
Drop down will have the following
ABC
DEF
GHI
Based on the selection i will have the data binded to the first list box from the database say for example my First list box is as follows
AB
CD
EF
Now if i select AB and click on Add Button provided it has to be added to the second list box and the database so that it should be removed and will be shown in second.
Now if i select CD my second list box will have the data as follows
AB
CD // I would like to select this item by default on clicking button as per given in the code so that only CD should be inserted
What i need is here to set the selected value to the next item
Here is my code

protected void Add_Click(object sender, ImageClickEventArgs e)
  {
    AdminPaytypes.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
    CustomerPaytypes.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
    if (AdminPaytypes.SelectedIndex >= 0)
    {
        for (int i = 0; i < AdminPaytypes.Items.Count; i++)
        {
            if (AdminPaytypes.Items[i].Selected)
            {
                if (!array.Contains(AdminPaytypes.Items[i]))
                {
                    array.Add(AdminPaytypes.Items[i]);
                }
            }
        }
        for (int i = 0; i < array.Count; i++)
        {
            if (!CustomerPaytypes.Items.Contains(((ListItem)array[i])))
            {
                CustomerPaytypes.Items.Add(((ListItem)array[i]));
  // AT this place i have to set the selected value to the array item i am getting
            }
            AdminPaytypes.Items.Remove(((ListItem)array[i]));
        }
    }
// This J loop also running twice but i have to insert only one item
    for (int j = 0; j < CustomerPaytypes.Items.Count; j++)
    {
        mlocal_strStoredProcName = "uspInsertCustomerPaytypes";
        oSysAdminGrantPaytypes.FedTaxID = ddlEmployer.SelectedValue;
        oSysAdminGrantPaytypes.PayFrequencyTypeID = CustomerPaytypes.SelectedItem.ToString();
        oSysAdminGrantPaytypes.PayFrequencyDesc = CustomerPaytypes.SelectedValue;
        oSysAdminGrantPaytypes.Insert(mlocal_strStoredProcName);
        //CustomerPaytypes.Items.Clear();
    }
}
Posted
Updated 23-Mar-11 2:19am
v2

1 solution

Only add and remove the selected item between two lists.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900