wrote:
> A Form has 2 select lists. The 1st one whose size is 5 (meaning 5
> options are shown at any given time) allows multiple selection whereas
> the 2nd one allows only 1 option to be selected at a time.
>
> When an option is selected in the 2nd select list, the ASP page posts
> itself. Assume that the 1st select list has the following 10 options
> (note that both the select lists are actually populated from 2
> different database tables):
>
> Australia
> Brazil
> Canada
> Denmark
> Egypt
> Finland
> Ghana
> Holland
> India
> Japan
>
> Suppose a user has selected Brazil, Finland & Holland in the 1st
> select list. Next the user selects an option in the 2nd select list.
> This causes the ASP page to post. Now after posting the page, I want
> the 1st select list to maintain its state so that Brazil, Finland &
> Holland remain selected (since the user had selected these 3 options
> in the 1st select list just before the page posted). This is how I
> tried it but it selects only the last option among the 3 options (i.e.
> after the page posts, only Holland remains selected):
>
> ===================================
> <%
> Dim arrLID,iEachLID,iLID,strLID,selFlag
>
> strLID=Request.Form("location")
>
> Dim objConn
> Set objConn=Server.CreateObject("ADODB.CONNECTION")
> 'open the connection using ConnectionString
>
> Dim strSQL
> strSQL="SELECT * FROM Locations"
http://www.aspfaq.com/show.asp?id=2096
>
> Dim objRS
> Set objRS=Server.CreateObject("ADODB.RECORDSET")
> objRS.Open strSQL,objConn
> %>
> <select name="location" multiple size=5>
> <%
> Do Until(objRS.EOF)
> If Not(IsEmpty(strLID)) Then
> arrLID=Split(strLID,", ")
> For Each iEachLID In arrLID
> iLID=iEachLID
> If(CInt(iLID)=CInt(objRS("LID"))) Then
> selFlag=" selected"
> Else
> selFlag=""
> End If
> Next
OK, the arrLID array contains Brazil, Finland and Holland. Walk through the
code. The first recordset item I assume is Australia (despite the lack of an
Order By clause on your query - you should not depend on the order of the
results without an Order By clause). None of the array items match, so
Australia remains unselected. Now Brazil gets tested. Brazil == Brazil, so
selFlag is set to "selected". Ah! but the loop is not finished! It now
compares Brazil to Finland, and, since they are not equal, it sets selFlag
back to ""!
The solution is simple, you need to break out of the For loop when a match
is found. Do this by adding Exit For immediately after setting selFlag to
"selected"
You may want to consider a more efficient solution:
Dim arrLID,iEachLID,iLID,strLID,selFlag
Dim arData, i, sLocation
strLID=Request.Form("location")
'Split this string ONCE, outside of the loop
If Not(IsEmpty(strLID)) Then
arrLID=Split(strLID,", ")
End IF
strSQL="SELECT LID,Location FROM Locations Order By LID"
Set objRS= objConn.Execute(strSQL,,1) '1=adCmdText
If Not objRS.EOF then arData = objRS.GetRows
objRS.Close:Set objRS=Nothing
'if you are done with the connection, close and destroy
'it here as well
if IsArray(arData) then
for i = 0 to ubound(arData,2)
iLID = CInt(arData(0,i)) 'if LID is int in the db Cint is not
needed
sLocation = arData(1,i)
selFlag = ""
if isArray(arrLID) then
for each iEachLID in arLID
if CInt(iEachLID) = iLID then
selFlag="selected"
exit for
end if
next 'iEachLID
end if
%>
<option value="<%= iLID%>"<%= selFlag %>><%=
sLocation%></option>
<%
next 'i
end if
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"