Solved

Multiple selections in dropdown

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

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>
</select>

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?
0
Comment
Question by:AS_SSUR
  • 2
4 Comments
 
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


0
 

Author Comment

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

Accepted Solution

by:
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>
0
 
LVL 28

Expert Comment

by:sybe
ID: 33452678
steps:

1. create an array from the string MyIDS

<%
arrMyIds = Split(myIDS, ",")
%>

2.
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
    Next
End Function


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

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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