views:

273

answers:

3

Hi,

Say I have a listbox lb, which I initialize using:

lb.DataSource = wordList \\wordList is a list of strings
lb.DataBind()

The listbox is initialized perfectly, and everything that should appear there does.

However, now I select one of items in the listbox with my mouse (coloring it dark blue, in case anyone wondered what I meant in "select"), and perform the following test:

 If lb.SelectedIndex <> -1 Then
        DoSomething()
 EndIf

or the test:

If Not lb.SelectedItem Is Nothing Then
            DoSomething()
     EndIf

etc..

Either way, when debugging it keeps not entering the if blocks, claiming of course that lb.selectedindext IS -1, lb.SelectedItem IS nothing, and so on.

What am I doint wrong? Why doesn't it recognise my selections?? 10x

A: 

Is your listbox setup as a multi-select listbox? If so, you have to use the SelectedItems collection to iterate through all the selected items.

Jason Berkan
A: 

You need to iterate items collection like in this way

foreach (ListItem item in Listbox1.Items)

{ if (item.Selected) { //do something

  }

}

this should be look, now I have write code for you in notepad, becoz right now I don't have .Net, But you can accomplish in this way

Muhammad Akhtar
A: 

It's possible that you do the DataBind() of the listBox also in the postback? Maybe you need to put it (the binding) in a

if (!IsPostback) { .. }

to ensure you're not losing your client's select.

tanathos
bingoooooooooooooooo :-)