DarrylGodden
DarrylGodden

Reputation: 1534

item.selected Not working in ListBox

I have this piece of code:

foreach (ListItem item in lbUnassigned.Items)
    {
        if (item.Selected)
        {
            string itemName = item.Text.ToString();
            string itemValue = item.Value.ToString();

            lbAssigned.Items.Add(new ListItem(itemName, itemValue));

            lbUnassigned.Items.Remove(lbUnassigned.SelectedItem);
        }
    }

Which is pretty much identical to an example given in the .NET 3.5 book I have, yet when stepping through this procedure the item.selected if false every time, even though I am selecting at least one value in the ListBox.

Any ideas what I could be doing wrong?

Upvotes: 0

Views: 2833

Answers (1)

Atzoya
Atzoya

Reputation: 1387

Usually when something like this happens the reason is control rebinding. Check if you have the if(!IsPostBack) on your page load when binding the control

Upvotes: 3

Related Questions