Dorababu Meka

Dorababu Meka

  • 227
  • 8.3k
  • 1.7m

How to set the selected value for a list box dynamically

Mar 23 2011 8:21 AM
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

Collapse
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();
}
}

Answers (3)