473,513 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Nov 19 '05 #1
2 6810
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!)
"bienwell" <bi******@hotmail.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
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.

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

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

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:ev*************@TK2MSFTNGP14.phx.gbl...
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!)
"bienwell" <bi******@hotmail.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1776
by: João Proença | last post by:
Ok the problem is this, when i try to receve the values from a form with the selection list below: <select name="jogos" size="5" multiple id="jogos"> <option value="1">Battlefield 1942</option>...
18
5674
by: booner | last post by:
I have a form that when it loads I would like to highlight the values (from a DB) that have been selected in a multiple selection list (<select multiple="true">. function onLoad() {...
0
1630
by: Jeff | last post by:
After I bind the repeater control in the form_load event, it builds multiple lines based on the number of rows in the dataset. In the repeater control, I have a textbox and a dropdown list box. ...
3
2498
by: User | last post by:
Form A (Main) Text Box 1 Text Box 2 Text Box 3 Form B (Pop-up) Choose a selection for Form A/Text Box 3 Scenario:
2
2259
by: Peter | last post by:
Hello! Please, could anyone tell, is it possible to set multiple items to be selected in list control in the code? For example when the web form is loaded three items of 5 are selected in list...
2
7036
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
5
1772
by: Bubba | last post by:
I have a dynamic pulldown list (ASP with javascript) that when a user picks a state, the corresponding counties for that state appear in a dynamic second pulldown list. When I submit the form, the...
4
4383
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
1
1994
by: Zak | last post by:
Hello, I need some help trying to grab the data selected from a checkboxlist. The checklist is acting like a menu. The first selection is accepted but not the multiple ones. For example is the...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.