Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple selections in dropdown

Posted on 2010-08-16
4
Medium Priority
?
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

618 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