How to get the values from multiple selection in the List Box (ASP.NET program)

K

Karl Seguin

Bienwell:

That's how you have to do it...selectedValue only returns the first
value...you need to loop throught hte items and get each value...

a couple comments on your code in general. FieldList.ITems.Count and
FieldList.SelectedIndex don't need to be CINT'ed...those are already int
values....Also, many people would say that a foreach is much more readable.
Two final thoughts, if you are expecting a lot of values, you should
probably use a StringBuilder...and you should make this a utility
function...so my code would look like:


Sub Button1_Click...
dim selectedValues = Utility.GetListControlSelectedValues(FieldList)
end sub

end class

Public Class Utility
Public Shared Function GetListControlSelectedValues(ByVal control As
ListControl) As String
If Control Is Nothing Then
Return ""
End If
Dim str As New System.Text.StringBuilder
For Each item As ListItem In control.Items
If item.Selected = True Then
str.Append(item.Text)
End If
Next
Return str.ToString()
End Function
End Class


Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
B

bienwell

Hi all,

I've got a problem with getting the data values from multiple selections in
the list box. I could only get the value and the index of the first item
from the selection, but not the others. The data loaded for the Listbox
(FieldList) is from data table. I already set the property
SelectionMode="Multiple" for the FieldList . Users perform multiple
selections from the list box. The event to get the value is in the
Button_Click. Here is my code:

Sub Button1_Click(sender As Object, e As EventArgs)
Dim strSel as string=""
Dim IndSel as string=""
Dim i as integer
For i=0 to CINT(FieldList.Items.Count) -1
If i= CINT(FieldList.SelectedIndex) Then
IndSel = IndSel & " " & i & " "
strSel=strSel & FieldList.selectedvalue & ", "
End If
Next
response.write("** IndSel ==> " & IndSel)
response.write("***StrSel ==> " & StrSel)

End Sub

Please take a look at my code and give me your advises. Thanks in advance.
 
B

bienwell

Thank you, Karl. You gave me the very quick answer. I've tried it and my
program works fine.

=================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top