Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Listbox woes - multiple selection

Status
Not open for further replies.
Mar 7, 2001
14
US
I have a listbox set up for multiple selection, and for some reason, I can't retrieve updated information on it. It is set up for multiple selection, and the check against it is triggered by a submit button. Everything is set as runat="Server" - so I'm not sure what I'm doing wrong. What I can say is that when running a check against it:

dim MyListItem as ListItem
dim MyString as String

for each MyListItem in MyListBox.Items
if MyListItem.Selected then
MyString = MyString + MyListItem.Value + ", "
end if
next

What is returned by the above code is the original state of the listbox - not the modified state. What do I need to do to ensure values are updated as the user performs selections?
 
Responding a bit quick on this but here is some code I use to capture multiple selections from a list box perhaps you can modify:

Sub btnNext_Click(Sender As Object, e As EventArgs)

Dim s As String
Dim r As String
Dim i, n As Integer

'Make sure at least one Monitor is selected...or collect the various ones selected...

n = 0
If listContacts.SelectedIndex <> -1 Then
If listContacts.SelectionMode = ListSelectionMode.Single Then
Me.txtCN.Text = listContacts.SelectedItem.Text
Me.txtCID.Text = listContacts.SelectedItem.Value
Else
For i = listContacts.SelectedIndex To listContacts.Items.Count - 1
If listContacts.Items(i).Selected Then
n = n + 1
If n = 1 Then
s &= listContacts.Items(i).Text
r &= listContacts.Items(i).Value
Else
s &= &quot;, &quot; & listContacts.Items(i).Text
r &= &quot;, &quot; & listContacts.Items(i).Value
End If
End If
Next
Me.txtCN.Text = s
Me.txtCID.Text = r
End If
End If
 
Thanks for the response! I'm taking a similar approach to this... I think what is happening has to do with the postback (e.g. my script that handles when the user hits &quot;Submit&quot; is occurring after(?) some sort of postback event). Again, I made sure that the listbox, it's routines, the form tag, and the script are set as runat=&quot;Server&quot;. But I have confirmed that when I do a loop to see what items in the collection are selected, I get the original values, not the modified ones.

R/
Mike
 
MNewKirk: Yes, I hear you. I've had similar problems. Need to be careful on the IsPostBack = True, False, etc... I have some additional listbox code which may help if you need it.
 
if you are using VB.NET then

MyString = MyString + MyListItem.Value + &quot;, &quot;

is not going to work

use

MyString = MyString & MyListItem.Value & &quot;, &quot;

instead

hth Daren J. Lahey
Just another computer guy...
 
Sorry, my bust. I am using MyString = MyString & MyListItem.Value. Thanks for the input though.

&quot;They never said it was going to be easy....&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top