altarEgo
asked on
easy - option selected match recordset value to comma separated string
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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(st rOne, "1"))%>>1
<option value="3"<%= SelectedIfTrue(IsInList(st rOne, "3"))%>3
<option value="7"<%= SelectedIfTrue(IsInList(st rOne, "7"))%>7
<option value="8"<%= SelectedIfTrue(IsInList(st rOne, "8"))%>8
<option value="11"<%= SelectedIfTrue(IsInList(st rOne, "11"))%>11
</select>
<input type="submit" name="test" />
</form>
<%
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(st
<option value="3"<%= SelectedIfTrue(IsInList(st
<option value="7"<%= SelectedIfTrue(IsInList(st
<option value="8"<%= SelectedIfTrue(IsInList(st
<option value="11"<%= SelectedIfTrue(IsInList(st
</select>
<input type="submit" name="test" />
</form>
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(st rOne, strOptions(intOption))) & ">1" & vbCrLf
Next
%>
</select>
<input type="submit" name="test" />
</form>
<%
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(st
Next
%>
</select>
<input type="submit" name="test" />
</form>
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].selectedIn dex = -1;
for(j=0; j< txts.length; j++){
txt = txts[j];
if(txt != "")
for(i=0; i < f.elements[fld].options.le ngth; i++)
if(f.elements[fld].options [i].value == txt)
f.elements[fld].options[i] .selected = true;
}
}
setcombovalues('test','tes t','<%= strOne %>');
//-->
</SCRIPT>
</BODY>
</HTML>
<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].selectedIn
for(j=0; j< txts.length; j++){
txt = txts[j];
if(txt != "")
for(i=0; i < f.elements[fld].options.le
if(f.elements[fld].options
f.elements[fld].options[i]
}
}
setcombovalues('test','tes
//-->
</SCRIPT>
</BODY>
</HTML>
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...
<%
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...
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...
<%
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...
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.
ASKER
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")%></OPT ION><%
rs.movenext
wend %>
</select>
<input type="submit" name="test" />
</form>
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")%></OPT
rs.movenext
wend %>
</select>
<input type="submit" name="test" />
</form>