iebidan Posted April 5, 2004 Posted April 5, 2004 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??? Quote Fat kids are harder to kidnap
Administrators PlausiblyDamp Posted April 5, 2004 Administrators Posted April 5, 2004 Are you databinding in your page_load event? If so are you doing the databinding on every page_load or using if not page.IsPostBack 'Databind first time end if kind of syntax? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MorningZ Posted April 5, 2004 Posted April 5, 2004 (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 April 5, 2004 by MorningZ Quote If you make it idiot proof, they'll build a better idiot :-)
iebidan Posted April 5, 2004 Author Posted April 5, 2004 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. Quote Fat kids are harder to kidnap
MorningZ Posted April 5, 2004 Posted April 5, 2004 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 Quote If you make it idiot proof, they'll build a better idiot :-)
iebidan Posted April 5, 2004 Author Posted April 5, 2004 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? Quote Fat kids are harder to kidnap
MorningZ Posted April 5, 2004 Posted April 5, 2004 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? Quote If you make it idiot proof, they'll build a better idiot :-)
Arch4ngel Posted April 5, 2004 Posted April 5, 2004 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 Quote "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
iebidan Posted April 5, 2004 Author Posted April 5, 2004 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!!!!! Quote Fat kids are harder to kidnap
Arch4ngel Posted April 6, 2004 Posted April 6, 2004 No prob man. Have fun ! :D Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.