ListBox

iebidan

Contributor
Joined
Feb 6, 2003
Messages
484
This is driving me nuts, I've a Multiple selection listbox in my application, after pressing a button the code is supposed to check what items I selected, the first item always come selected.
Now, if I select other items and unselect the first one the code will say that the first item is selected and will not check for the others, if I leave the first item selected the code will check for the other items.
The code I use to check what items are selected is

Code:
Dim li As ListItem
For Each li In LbRoles.Items
     If li.Selected = True Then
          Roles += li.Value.ToString & ","
     End If
Next

What's wrong with this? how can I make this code to evaluate what items I selected the correct way???? why if I check other items will say that the first one is selected??? if I select the first one will check the other items???
 
Are you databinding in your page_load event? If so are you doing the databinding on every page_load or using
Visual Basic:
if not page.IsPostBack
'Databind first time 
end if
kind of syntax?
 
i do a lot of coding with the "muliple select" listbox... and i look for selections like this

Code:
Dim strSelections As String
Dim i As Integer
For i = 0 to (lstBox.Items.Count - 1)
     If lstBox.Items(i).Selected Then
          if strSelections <> "" then strSelections += ","
          strSelections += lstBox.Items(i).Value
     End If
Next
 
Last edited:
Well, I tried MorningZ's code and the result is the same, even if the first item is unselected the code keeps saying it's selected and the items selected are evaluated as false.
No PlausiblyDamp is not a postback, the evaluation is done in a click event of a button before I redirect the user to a new page according on what he / she selected on the listbox.
 
you've got some other coding issues then......

that code i posted works (as should the code you posted)

either you are screwing up databinding to the listbox, or something else
 
this si the code I use to add the listitems
Code:
Dim rd as SqlDataReader
qry = "SELECT * FROM T002_C_CATALOG_OPC WHERE C_CATALOG = 43"
rd = fnGet(qry)
LbRoles.Items.Clear()
LbRoles.Items.Add(New ListItem("ALL", 0))
While rd.Read
     If rd.GetValue(2).ToString <> "0" Then
          LbRoles.Items.Add(New ListItem(rd.GetValue(3).ToString.Trim, rd.GetValue(2).ToString.Trim))
     End If
End While
rd.Close()
LbRoles.SelectedIndex = 0

is there something wrong with this?
 
iebidan said:
this si the code I use to add the listitems
Code:
Dim rd as SqlDataReader
qry = "SELECT * FROM T002_C_CATALOG_OPC WHERE C_CATALOG = 43"
rd = fnGet(qry)
LbRoles.Items.Clear()
LbRoles.Items.Add(New ListItem("ALL", 0))
While rd.Read
     If rd.GetValue(2).ToString <> "0" Then
          LbRoles.Items.Add(New ListItem(rd.GetValue(3).ToString.Trim, rd.GetValue(2).ToString.Trim))
     End If
End While
rd.Close()
LbRoles.SelectedIndex = 0

is there something wrong with this?
yeah, that code you just posted could be a lot bette (for instance, why not just have your SQL statement filter out all of column 2's that aren't zero? then you can avoid the uneccessary check in the code), but totally unrelated to the problem at hand

what about actually calling this code? is it getting called on every single pageload, postback or no postback (it should only be called from Page_Load and wrapped inside If Not Page.IsPostback .... End If)

and maybe a stupid question, but have you utilized tools like Trace.Write and stuff to spit out what the code sees during execution?
 
Is this done on Page_Load or the OnClick of a button ?

if this is in Page_Load...
add the following :

If Not Me.IsPostBack Then
[YOUR CODE]
End If

the LbRoles.Items.Clear() might clear your selection if it's in postback
 
OMG, the code from Arch4ngel worked, in the Load event I use the if Not me.IsPostBack and things started to work correctly, THANKS GUYS!!!!!
 
Back
Top