473,508 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Select List

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"

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
Else
selFlag=""
End If
%>
<option value="<%= objRS("LID") %>"<%= selFlag %>><%=
objRS("Location") %></option>
<%
objRS.MoveNext
Loop
%>
</select>
===================================

Can someone please point out what am I missing in the above code?

Apr 27 '07 #1
4 4381
rn**@rediffmail.com 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"
Apr 27 '07 #2
On Apr 26, 7:18 pm, "Bob Barrows [MVP]" <reb01...@NOyahoo.SPAMcom>
wrote:
r...@rediffmail.com 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"- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Bob, it was indeed very very kind of you to show a more efficient way
to do the same thing. Thanks a lot.

Regards,

RON

Apr 27 '07 #3
On Apr 26, 7:40 pm, r...@rediffmail.com wrote:
On Apr 26, 7:18 pm, "Bob Barrows [MVP]" <reb01...@NOyahoo.SPAMcom>
wrote:


r...@rediffmail.com 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"- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

Bob, it was indeed very very kind of you to show a more efficient way
to do the same thing. Thanks a lot.

Regards,

RON- Hide quoted text -

- Show quoted text -
Bob, I encountered a new problem with the 1st select list.

Suppose Brazil, Finland & Holland are selected in the 1st select list.
Next I select an option from the 2nd select list; the page posts & the
1st select list shows Brazil, Finland & Holland selected.

After this, I again select a different option in the 2nd select list;
again the page gets posted but surprisingly, the options selected in
the 1st select list i.e. Brazil, Finland & Holland no longer remain
selected. In fact, when I click the submit button, the action page
just retrieves an empty string i.e. Request.Form("location") is just
an empty string instead of the location ids (objRS("LID")) of the 3
locations!

Why is the 1st select list losing its state under such circumstances?

Please note that when an option is selected in the 2nd selected list,
the page posts to itself (which I have done using JavaScript) but when
the submit button is clicked, the page posts to another page.

Apr 27 '07 #4
rn**@rediffmail.com wrote:
<snip - please snip irrelevant text>
Bob, I encountered a new problem with the 1st select list.

Suppose Brazil, Finland & Holland are selected in the 1st select list.
Next I select an option from the 2nd select list; the page posts & the
1st select list shows Brazil, Finland & Holland selected.

After this, I again select a different option in the 2nd select list;
again the page gets posted but surprisingly, the options selected in
the 1st select list i.e. Brazil, Finland & Holland no longer remain
selected. In fact, when I click the submit button, the action page
just retrieves an empty string i.e. Request.Form("location") is just
an empty string instead of the location ids (objRS("LID")) of the 3
locations!

Why is the 1st select list losing its state under such circumstances?

Please note that when an option is selected in the 2nd selected list,
the page posts to itself (which I have done using JavaScript) but when
the submit button is clicked, the page posts to another page.
It's got to have something to do with the way the form is getting submitted.
Help us out by posting a small repro. Create a new pair of test pages that
contain nothing but the code needed to reproduce this problem and post them
here. We need to run your code in order to test it, and we do not have your
database, so hardcode the option list in the second select element and
substitute an ad hoc recordset for the recordset actually used in your
actual code to populate the first select element:

Dim objRS
const adInteger=3
const adVarChar = 200
Set objRS = createobject("adodb.recordset")
With objRS.Fields
.Append "LID",adInteger
.Append "Location",adVarChar,100
End With
objRS.Open
With objRS
.AddNew Array("LID","Location"), Array(1,"Australia")
.AddNew Array("LID","Location"), Array(2,"Brazil")
'etc.
End With
The second test page will likely contain only this code:
<%
Response.Write "Request.Form(""location"") contains """ & _
Request.Form("location")

--
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"
Apr 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1882
by: rich | last post by:
I have a database with 1 to many and the many is a list with multiple selects in a list. When I click on a master record I have as part of my form the select statement for the multiple choice...
3
12534
by: imrantbd | last post by:
This is my first problem.Please help me. I have the following code: <head> <script language="JavaScript"> function addSrcToDestList() { destList1 = window.document.forms.destList; srcList...
3
8701
by: imrantbd | last post by:
I need array type name like "destList" must use for my destlist select box,not a single name.Or need a solution to capture multiple value of "destList" select box and send all selected value in php...
1
3583
by: Jeff Gardner | last post by:
Greetings: I have a table with 3 pieces of data that I would like to use to dynamically populate 3 drop downs using javascript. The fields are state, orgname, office. If it's not already...
3
14167
by: swethasivaram | last post by:
Hello I have a multiple select list as follows: <select name="selList" multiple> <option value="1"Item 1</option> <option value="2"Item 2</option> <option value="3"Item 3</option> </select>
2
5104
by: billa856 | last post by:
Hi, My Project is in MS Access. In that I have one form in which I have some textboxes,comboboxes and listboxes. Now when I select value from 1st combobox(CustomerID) then it wil generate list for...
2
1806
by: permander kumar | last post by:
hi plz help me, i have a code in which two multiple selection dropdown list. in first list if I am select single value the data are fetch in second table on button click. but the problem is in...
1
4866
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
6
4653
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.