Multiple selections in dropdown

Posted on 2010-08-16
Last Modified: 2012-05-10
I have a comma delimited list of Id's called "myIDS". It contains lists such as:

I create an ASP dropdown list such as:

<select name="shipList" class="input" size="6" multiple>
<option value="4" >Name1</option>
<option value="11" >Name2</option>
<option value="30" >Name3</option>
<option value="34" >Name4</option>
<option value="26" >Name5</option>
<option value="8" >Name6</option>
<option value="13" >Name7</option>

I need to set the options in the dropdown to "Selected" if the value is in the comma delimited list = the values in the list. Keep in mind, the dropdown is generated dynamically from a DB.

Any ideas?
Question by:AS_SSUR
  • 2
LVL 22

Expert Comment

by:Om Prakash
ID: 33451349
you can use instr to do this
<option value="4" <% IF InStr(myIDS,"4") > 0 THEN response.write " selected" %>>Name1</option>
<option value="11" <% IF InStr(myIDS,"11") > 0 THEN response.write " selected" %>>Name2</option>

similarly you can add for other options


Author Comment

ID: 33451839
I tried "inStr" but 4 and 14 both have a "4" in the Str.
LVL 22

Accepted Solution

Om Prakash earned 500 total points
ID: 33451928
dim myIDS
myIDS = "4,11,30,"

In this case 4 will be selectetd but 14 and 41 will not

<option value="4" <% IF InStr(myIDS,"4") > 0 THEN response.write " selected" %>>Name4</option>
<option value="41" <% IF InStr(myIDS,"41") > 0 THEN response.write " selected" %>>Name41</option>
<option value="14" <% IF InStr(myIDS,"14") > 0 THEN response.write " selected" %>>Name14</option>
LVL 28

Expert Comment

ID: 33452678

1. create an array from the string MyIDS

arrMyIds = Split(myIDS, ",")

Use attached function IsElementOfArray() to check if it's there. Oh, and I have a standard function IIF(), which is quite useful (and present in most languages, but not in VBScript)

<option value="4" <%=IIF(IsElementOfArray("4", arrMyIds, "selected")%>>Name4</option>
<option value="14" <%=IIF(IsElementOfArray("14", arrMyIds, "selected")%>>Name4</option>

Note that it makes a different if "4" is a String or a Number, because "4" = 4 returns False.


Alternatively, if you want to use the Instr() method as above, use it as this:

' encapsulate thew myIDS string with comma's
myIDS = "," & myIDS & ","

' now search for ",4," or ",14," etc

<option value="4" <% IF InStr(myIDS,",4,") > 0 THEN response.write " selected" %>>Name4</option>

Function IsElementOfArray(ByVal v, ByVal a)
    Dim e
    IsElementOfArray = False
    If Not IsArray(a) Then Exit Function
    For Each e In a
        If v = e Then IsElementOfArray = True: Exit For
End Function

Function IIf(bCondition, TrueValue, FalseValue)
    IIf = FalseValue
    If bCondition Then IIf = TrueValue
End Function

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB Script: create txt file with DateStamp and IP Adress 6 27
Import Data from Multiple Text Files in Excel 12 63
Insert Button on a table 16 38
VBA Vbscript  Issue 9 27
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question