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
Comment Utility
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

Comment Utility
I tried "inStr" but 4 and 14 both have a "4" in the Str.
LVL 22

Accepted Solution

Om Prakash earned 500 total points
Comment Utility
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

Comment Utility

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now