Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

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???

Fat kids are harder to kidnap
Posted (edited)

i do a lot of coding with the "muliple select" listbox... and i look for selections like this

 

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

Edited by MorningZ
If you make it idiot proof, they'll build a better idiot :-)
Posted

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.

Fat kids are harder to kidnap
Posted

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

If you make it idiot proof, they'll build a better idiot :-)
Posted

this si the code I use to add the listitems

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?

Fat kids are harder to kidnap
Posted
this si the code I use to add the listitems

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?

If you make it idiot proof, they'll build a better idiot :-)
Posted

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

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted
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!!!!!
Fat kids are harder to kidnap
Posted
No prob man. Have fun ! :D

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...