0

I have the following listbox on aspx page.

  <asp:ListBox runat="server" ID="lbA" Visible="true" 
                SelectionMode="Multiple" DataTextField="A_FACTOR" 
                DataValueField="A_ID" Width="218px"> </asp:ListBox>  

then on code behind I have the following on a button click I have the following.

string aFactor = "";

 foreach(ListItem listItem in lbA.Items)
    {
        if (listItem.Selected)
        {
            aFactor += listItem.Value + ",";
        }
    } 

when I select values (any values) from the listbox, the listItem.Selected is always false and I am not able to get values because of that. Any idea what is wrong?

kalls
  • 2,797
  • 17
  • 55
  • 75

2 Answers2

0

populate the list in

if (!IsPostBack)
{
}

at the time of form load

kleopatra
  • 51,061
  • 28
  • 99
  • 211
0

As I thought, you were doing something wrong outside the code you have provided, please see the answer of this link :

Selected item in list box is null

Community
  • 1
  • 1
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • I had outside !Page.IsPostBack. :( I was over confident with my code. I learnt my lesson. – kalls Jan 12 '12 at 16:12