Solved

easy - option selected match recordset value to comma separated string

Posted on 2004-09-27
9
348 Views
Last Modified: 2008-01-09
experts,

my question is how do i get option selected right when i'm comparing a comma separated string to an
integer value in a record set?  this is vbscript and asp.

strOne = "1,9,11,3,8,12" is a request value

iterating the recordset builds  a form like this

<form name="test">
<select multiple name="test">
<option value="1"> 1
<option value="3">3
<option value="7">7
<option value="8">8
<option value="11">11
<input type="submit" name="test" />
</form>

How do i detect whether a value in the string matches a value from the recordset and then
set the option selected to true?

TIA
0
Comment
Question by:altarEgo
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 5

Accepted Solution

by:
darksinclair earned 125 total points
ID: 12165207
<SCRIPT>
var theValues = new Array();
function getSelectedValues( )
{
      for( i = 0; i < document.testFrm.testSlct.length; i++ )
      {
            for( j = 0; j < theValues.length; j++ )
            {
                  if( theValues[j] == document.testFrm.testSlct.options[i].value )
                  {
                        document.testFrm.testSlct.options[i].selected = true;
                  }
            }
      }
}
</SCRIPT>

<%
dim strOne
strOne = "1,9,11,3,8,12"
dim valuesArray
valuesArray = Split(strOne,",")
for i = 0 to UBound( valuesArray )
%>
      <SCRIPT>theValues.push( '<%=valuesArray(i)%>' );</SCRIPT>
<%
next
%>
<HTML>
<BODY onLoad="getSelectedValues();">
<form name="testFrm">
<select multiple name="testSlct">
<option value="1"> 1
<option value="3">3
<option value="7">7
<option value="8">8
<option value="11">11
<input type="submit" name="test" />
</form>
</BODY>
</HTML>

Try that on for size mate.. let me know if it swhat you rlooking for.  Basically I create an ASP array of all the values in your CSV string.. and then I loop through the array and make a javascript array.  Through Javascript you can easily compare the values in the select box and the array and check to see if the values are in there and select them if they are.


Cheers!
0
 
LVL 5

Expert Comment

by:darksinclair
ID: 12165211
This was a fun one ^_^  made me use a lil bit of creativity.. probably not the most efficient since im kinda wasting an ASP array.. but it's the only thing I could think of.. I bet someone else will have somethinga little more ingenious to offer!
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12165451
Another possibility:

    <%
    Function IsInList(strList, strItem)
        IsInList = ( InStr(1, strList, "," & strItem & ",") <> 0 )
    End Function

    Function SelectedIfTrue(bln)
        If bln Then
            SelectedIfTrue = " selected"
        Else
            SelectedIfTrue = ""
        End If
    End Function


    strOne = "," & Request.QueryString("test") & ","
    %>

    <form name="test">
    <select multiple name="test">
        <option value="1"<%= SelectedIfTrue(IsInList(strOne, "1"))%>>1
        <option value="3"<%= SelectedIfTrue(IsInList(strOne, "3"))%>3
        <option value="7"<%= SelectedIfTrue(IsInList(strOne, "7"))%>7
        <option value="8"<%= SelectedIfTrue(IsInList(strOne, "8"))%>8
        <option value="11"<%= SelectedIfTrue(IsInList(strOne, "11"))%>11
    </select>
    <input type="submit" name="test" />
    </form>
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 15

Expert Comment

by:Thogek
ID: 12165463
Or:

    <%
    Function IsInList(strList, strItem)
        IsInList = ( InStr(1, strList, "," & strItem & ",") <> 0 )
    End Function

    Function SelectedIfTrue(bln)
        If bln Then
            SelectedIfTrue = " selected"
        Else
            SelectedIfTrue = ""
        End If
    End Function


    strOne = "," & Request.QueryString("test") & ","
    %>

    <form name="test">
    <select multiple name="test">
    <%
    strOptions = Array("1", "3", "7", "8", "11")
    For intOption = LBound(strOptions) to UBound(strOptions)
        Response.Write "<option value=""" & strOptions(intOption) & """" & SelectedIfTrue(IsInList(strOne, strOptions(intOption))) & ">1" & vbCrLf
    Next
    %>
    </select>
    <input type="submit" name="test" />
    </form>
0
 
LVL 5

Expert Comment

by:ajitanand
ID: 12166442
My preffered way is to pass on the string processing and comparisons to the client side browser, through JavaScript using a generic re-usable function:


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<%
strOne = "1,9,11,3,8,12"' is a request value
%>
<form name="test">
<select multiple name="test">
      <option value="1"> 1
      <option value="3">3
      <option value="7">7
      <option value="8">8
      <option value="11">11
</select>
<input type="submit" name="btnSubmit" />
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
function setcombovalues(frm,fld,txt) {
      var f = document.forms[frm];
      txts = txt.split(",");
      f.elements[fld].selectedIndex = -1;
      for(j=0; j< txts.length; j++){
            txt = txts[j];
            if(txt != "")
                  for(i=0; i < f.elements[fld].options.length; i++)
                        if(f.elements[fld].options[i].value == txt)
                              f.elements[fld].options[i].selected = true;
      }
}

setcombovalues('test','test','<%= strOne %>');
//-->
</SCRIPT>
</BODY>
</HTML>
0
 
LVL 15

Expert Comment

by:gladxml
ID: 12167232
You can also try the code below...

<%
  Function setSelect(a,b)
  arrayRequest = Split(b,",")
  for i = LBound(arrayRequest) to UBound(arrayRequest)
  if trim(a) = trim(arrayRequest(i)) then
  setSelect = "selected"
  end if
  next
  end function
 %>
<form name="test">
<select multiple name="test">
<%
'assuming that your requestobject name is strOne
strOne  = Request.form("strOne")
%>
<option value="1"<%= setSelect("1",strOne) %>> 1
<option value="3"<%= setSelect("3",strOne) %>>3
<option value="7"<%= setSelect("7",strOne) %>>7
<option value="8"<%= setSelect("8",strOne) %>>8
<option value="11"<%= setSelect("11",strOne) %>>11
<input type="submit" name="test" />
</form>


Happy programming...
0
 
LVL 15

Expert Comment

by:gladxml
ID: 12167392
Ohh try to use this function instead to make it more efficient and faster... I forgot to put "exit for" if there is a match...

<%
  Function setSelect(a,b)
  arrayRequest = Split(b,",")
  for i = LBound(arrayRequest) to UBound(arrayRequest)
  if trim(a) = trim(arrayRequest(i)) then
  setSelect = "selected"
  exit for
  end if
  next
  end function
 %>

Happy programming...
0
 
LVL 5

Expert Comment

by:ajitanand
ID: 12167550
One more point - The code I gave above works well with multiple select listboxes as well. And it is good for even listboxes with hundreds of entries efficiently as processing is done on the client side.
0
 
LVL 1

Author Comment

by:altarEgo
ID: 12169782
There are lots of ways to do this, I see.  This is what I came up with:

strOne = "1,9,11,3,8,12"
strOne = replace(strOne, ",", "$")
strOne = "$" & strOne & "$"
%>
<form name="test">
<select multiple name="test"><%

while rs.eof=FALSE
      strTemp = "$" & rs("response_seq") & "$"
%>
      <option value="<%= rs("response_seq")%>" <% if instr(strOne, strTemp) then response.write "SELECTED" end if %>><%= rs("response_name")%></OPTION><%
      rs.movenext
      wend %>
</select>
<input type="submit" name="test" />
</form>
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

776 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