Friday, March 16, 2012

listbox count

I would like to be able to retrieve the text of the selected items in a listbox in asp.net 2.0
This is what I am doing by it is not correct.
Thanks

Dim i As Integer
For i = 0 To lstPA.Items.Count
If lstPA.SelectedItem.Selected Then
Dim x As String = lstPA.SelectedItem.ToString()
Dim y As String = lstPA.SelectedValue.ToString()
End If
NextI'm confused at what you are trying to do, but you have lot's of mistakes in that. Don't loop to count, loop to count - 1 as the list is zero based. If you want to look at an item in the list then, use:

lstPA.Items(i)

So you can say :

if lstPA.Items(i).Selected then

.SelectedItem is a list item, so it won't covert to a string. Do

lstPA.SelectedItem.Text or lstPA.SelectedItem.Value

Also don't declare your strings inside the loop if you want them to be useable anywhere outside the loop.

0 comments:

Post a Comment