Click to See Complete Forum and Search --> : Retain multiple selects


ktri
10-20-2006, 04:58 PM
I have a multiple select box on a form. It gets the value and text from an Oracle database table. I collect the selections, move to another asp page, perform actions and return to the calling asp page.

When I return to the calling page, I need to repopulate the values and text in the multiple select box from the database and reselect the options the user selected the first time the page was viewed.

I am getting the selected values from the query string as a comma separated string which I then split into an array with the split function. Can't quite figure out how to loop thru the recordset and the array to show all, but show as selected only the values from my split array. The following code does not work, but can be used as a starting point.

'get val from QS
strDepts = Request.QueryString("sdept")
if len(strDepts) > 0 and not isNull(strDepts) and strDepts <> "" then
'split
arrSelectedDepts = split(strDepts,",")
end if
<td><select name="selDept" id="selDept" title="Dept" style="width:120" size="5" multiple="multiple" >
<%strSQL = "SELECT lngDeptID, txtDeptCode " _
& " FROM EDS.tblDepartments " _
& " WHERE blnInactive = 'N' " _
& " ORDER BY txtDeptCode"
'on error resume next
set objRS = objConn.Execute(strSQL)
'CheckErrors objConn, "other"
if not objRS.EOF then
do while not objRS.EOF
'assign vals from db
lngDeptID = objRS("lngDeptID")
strDeptCode = objRS("txtDeptCode")
if not isEmpty(arrSelectedDepts) then
for intCount = 0 to ubound(arrSelectedDepts)
if clng(arrSelectedDepts(intCount)) = clng(lngDeptID) then%>
<option selected="selected" value="<%=lngDeptID%>"><%=strDeptCode%></option>
<%else%>
<option value="<%=lngDeptID%>"><%=strDeptCode%></option>
<%end if
next
else%>
<option value="<%=lngDeptID%>"><%=strDeptCode%></option>
<%end if
objRS.MoveNext
loop
else%>
<option selected="selected" value="0">Choose Dept</option>
<%end if%>
</select>
</td>

I.m.I
10-21-2006, 08:20 PM
i made for u an example i hope that can help u
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<%
if request.Form("a")<>"" then
arr=split(request.Form("s"),",")

end if
%>
<body>
<form action="" method="post">
<select multiple name="s">
<%i=0

do until i=10
%>
<option value="<%=i%>" <%
if arr(0)<>"" then
for n=0 to ubound(arr)
if cint(arr(n))=i then response.Write("selected")
next
end if
%>><%=i%></option>
<%
i=i+1
loop

%>
</select>
<input type="submit" value="a" name="a">
</form>
</body>
</html>