Multiple Selection
Enable Multiple Selection in a list field.
Original Author: Bhushan-
Code
You selected <%= intNumberSelected %> team(s). Request.Form("teams") returned: <%= strSelectedTeams %> You can easily convert this to an array using the split command. * ' Save this file as multiple_selection.asp
<%
Dim intNumberSelected ' Count of items selected
Dim strSelectedTeams ' String returned from QS (or Form)
Dim arrSelectedTeams ' Variable to hold team array
Dim I ' Looping variable
' Retrieve the count of items selected
intNumberSelected = Request.Form("teams").Count
if intNumberSelected = 0 Then
%>
Pick your favorite teams (hold down CTRL to select more than one):
<%
Else
' Retrieve the comma delimited list of teams that is returned
' from the Form collection. This could also be gotten from
' the QueryString collection, but I used the post method
' instead of get in my form.
strSelectedTeams = Request.Form("teams")
' Split our text variable into an array so we have easy
' programmatic access to the individual elements. Rememeber
' the array will start at 0 not 1 so a 10 item array will
' run from 0 to 9 and not 1 to 10!
' Split takes a string and then searches for a delimiter
' (in this case the comma followed by a space) in that string.
' It returns an array of strings which consists of all the
' text except the delimiters cut up into nice little pieces
' at the delimiters. The last two parameters specify the
' maximum number of delimiters to find (-1 = all) and the last
' one is what type of comparison to perform
' (0 = binary comparison, 1 = text comparison)
arrSelectedTeams = Split(strSelectedTeams, ", ", -1, 1)
' UPDATE NOTE:
' One of our ever-vigilant visitors pointed out to me that this will cause problems
' if any of yourchoices contain a comma. While he's right, I'm leaving the code
' as is,because I feel exposing users to the split command and some array work
' is a goodthing, but if you need to use commas try something like this:
'ReDim arrSelectedTeams(intNumberSelected - 1)
'For I = 1 To intNumberSelected
' arrSelectedTeams(I - 1) = Request.Form("teams")(I)
'Next 'I
' We now join our regularly scheduled program already in progress...
' Show users the count of and string containing their choices
%>
The contents of that array are shown in the table below:
Array Element *
Value
<%
' Some debugging lines if you start having problems
'Response.Write LBound(arrSelectedTeams)
'Response.Write UBound(arrSelectedTeams)
' Loop through the array showing one table row for each selection
For I = LBound(arrSelectedTeams) To UBound(arrSelectedTeams)
%>
<%= I %>
<%= arrSelectedTeams(I) %>
<%
Next 'I
%>
Remember that VBScript arrays start counting from 0.
So a 10 item array will run from 0 to 9!
<%
' Some code showing fully qualified requests. Might be fun to
' play with or possible useful for debugging.
'Dim Item
'For Each Item in Request.Form
' Response.Write Request.Form.Key(Item) & ": "
' Response.Write Request.Form.Item(Item) & " "
' Response.Write Request.Form.Item(Item).Count & "
"
'Next
End If
%>
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.