Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Requesting Listbox data

Status
Not open for further replies.

Lbob

Programmer
May 23, 2003
157
GB
I'm trying to read the values from a listbox and I can't get it it to work. Can anyone help?
I want to be able to pass through all records displayed in this listbox


<form name=&quot;frmAggregate&quot; method=&quot;post&quot; action=&quot;aggregate_confirm.asp&quot;>
<SELECT Name=&quot;LstAccounts&quot; size=10 style=&quot;HEIGHT: 275px; WIDTH: 250px&quot; >

<%

dim rs

set rs = server.CreateObject(&quot;ADODB.Recordset&quot;)

set rs = ListClients()

do until rs.eof
Response.Write &quot;<option value=&quot; & rs(&quot;GRID&quot;) & &quot;>&quot; & rs(&quot;ClientName&quot;) & &quot;</option>&quot;
rs.MoveNext
loop
rs.Close

set rs = nothing
%>

</SELECT>
</form>


So in aggregate_confirm.asp
I try the following which doesn't work

arrClients = Split(Request.Form(&quot;LstAccounts&quot;),&quot;, &quot;)

For i = 0 to iClients
Response.Write arrClients(i) & &quot;<br>&quot;
Next
 
When you say it doesn't work, does the Request.Form come back with an empty string (doesn't pass your value) or ...?

Also, you may want to add soem quotes to your options. With a name like GRID I doubt there are going to be any spaces, but you should get in the bait of using quotes simply because it will bring you a step closer to being XHTML compliant (which browsers parse more efficiently):
Code:
Response.Write &quot;<option value=&quot;&quot;&quot; & rs(&quot;GRID&quot;) & &quot;&quot;&quot;>&quot; & rs(&quot;ClientName&quot;) & &quot;</option>&quot;

Also, it appears you want to be able to make multiple selections from the select box. If this is the case you need to add a multiple attribute to the select tag:
Code:
<SELECT Name=&quot;LstAccounts&quot; size=10 style=&quot;HEIGHT: 275px; WIDTH: 250px&quot; multiple>
or
Code:
<SELECT Name=&quot;LstAccounts&quot; size=&quot;10&quot; style=&quot;HEIGHT: 275px; WIDTH: 250px&quot; multiple=&quot;multiple&quot;>

Also, I don't see a submit button inside your form, how are you submitting it?

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Do you know how hot your computer is running at home? I do
 
there was an error i noticed:
Response.Write &quot;<option value='&quot; & rs(&quot;GRID&quot;) & &quot;'>&quot; & rs(&quot;ClientName&quot;) & &quot;</option>&quot;

change this an see if the error still persists...
it yes what is the error?


Known is handfull, Unknown is worldfull
 
if the listdata is not selected would'nt the FORM have no values ? I debug it

ooooooooooooooooooooooooh bugger.
 
oops! I actually copied the wrong piece of code, this is what I've got on my page

<SCRIPT LANGUAGE='JavaScript'>
<!--

function submit_aggregate()
{ //Called on Form submit button, need to put some validation in
document.frmAggregate.submit() //Submit the form
}
function AddSubAccount()
{
var f = document.forms[0];
var idx = f.LstAllAccounts.selectedIndex;
var sAccount = f.LstAllAccounts.options[idx].text;
var iAccount = f.LstAllAccounts.options[idx].value;
var iThere = 0;
var iRows = f.LstSubAccounts.options.length;
var newOpt = document.createElement(&quot;OPTION&quot;);
newOpt.text = sAccount;
newOpt.value = iAccount;

if (iRows != 0) {
for (var i = 0; i < iRows ; i++){
if (f.LstSubAccounts.options.value == iAccount){
var iThere = 1;
break
}
}
if (iThere != 1){
f.LstSubAccounts.add(newOpt);
f.TxtSubAccounts.value = iRows + 1
}
} else if(iRows == 0){
f.LstSubAccounts.add(newOpt);
f.TxtSubAccounts.value = iRows + 1
}
}

-->
</SCRIPT>


<form name=&quot;frmAggregate&quot; method=&quot;post&quot; action=&quot;aggregate_confirm.asp&quot;>
<table width=&quot;600&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td><b>Master Account:</b></td><td><INPUT Name=&quot;TxtAggregate&quot; size=&quot;66&quot;></td>
</tr>
</table>
<table width=&quot;600&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr><td colspan=&quot;3&quot;>&nbsp;</td></tr>
<tr><td><b>All Clients</b></td><td>&nbsp;</td><td><b>Selected Clients</b></td></tr>
<tr>
<td><SELECT Name=&quot;LstAllAccounts&quot; size=10 style=&quot;HEIGHT: 275px; WIDTH: 250px&quot; ondblClick=&quot;AddSubAccount()&quot;>

<%

dim rs

set rs = server.CreateObject(&quot;ADODB.Recordset&quot;)

set rs = ListClients()

do until rs.eof
Response.Write &quot;<option value=&quot; & rs(&quot;GRID&quot;) & &quot;>&quot; & rs(&quot;ClientName&quot;) & &quot;</option>&quot;
rs.MoveNext
loop
rs.Close

set rs = nothing
%>

</SELECT>
</td>
<td align=&quot;center&quot;><input type=&quot;button&quot; name=&quot;add&quot; value=&quot;Add&quot; onclick=&quot;AddSubAccount()&quot;><BR><BR><BR><input type=&quot;button&quot; name=&quot;remove&quot; value=&quot;Remove&quot; onclick=&quot;RemoveSubAccount()&quot;><BR><BR><input type=&quot;hidden&quot; name=&quot;TxtSubAccounts&quot; value=0></td>
<td>
<SELECT Name=&quot;LstSubAccounts&quot; size=10 style=&quot;HEIGHT: 275px; WIDTH: 250px&quot; ondblClick=&quot;RemoveSubAccount()&quot;>
</SELECT>
</td>
</tr>
<tr><td colspan=&quot;3<a href=&quot;javascript:submit_aggregate()&quot;><img align=&quot;top&quot; border=&quot;0&quot; src=&quot;graphics/go.gif&quot; WIDTH=&quot;24&quot; HEIGHT=&quot;19&quot;></a></td></tr>
</table>
</form>

Then on the aggregate_confirm.asp page

arrClients = Split(Request.Form(&quot;LstAccounts&quot;),&quot;, &quot;)

For i = 0 to iClients
Response.Write arrClients(i) & &quot;<br>&quot;
Next


 
I just noticed something. You say you want to pass all the records in that listbox. If that is the case you will need to select them all. The easiest way to do that would be to make the listbox multiple, then write a &quot;selected&quot; inside each options, like so:
Code:
Response.Write &quot;<option value=&quot;&quot;&quot; & rs(&quot;GRID&quot;) & &quot;&quot;&quot; selected>&quot; & rs(&quot;ClientName&quot;) & &quot;</option>&quot;

That will make al the values selected to start with...of course your user could deslect some of them.

I'm not sure I understand what your tryiong to do. If you want to pass them all it would be easier to just select them all from the database. If you want the user to select a few and then read their selections, the changes from my post above should be enough.

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Do you know how hot your computer is running at home? I do
 
Thats correct Tarwn.. use selected to get all the data back

tested with this.

<form name=&quot;frmAggregate&quot; method=&quot;post&quot; action=&quot;confirm.asp&quot;>
<SELECT Name=&quot;LstAccounts&quot; size=10 style=&quot;HEIGHT: 275px; WIDTH: 250px&quot; multiple=true >


<%
dim arr
arr = array(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;)

for i = 0 to UBound(arr)
Response.Write &quot;<option value=&quot; & arr(i) & &quot; selected>&quot; & i & &quot;</option>&quot;
next

%>

</SELECT>
<input type=submit >
</form>
<%
%>

YOU MUST USE THE UBOUND FUNCTION FOR ARR SIZES

<%
arrClients = Split(Request.Form(&quot;LstAccounts&quot;),&quot;, &quot;)

For i = 0 to UBound(arrClients)
Response.Write arrClients(i) & &quot;<br>&quot;
Next
%>

ooooooooooooooooooooooooh bugger.
 
I don't understand why they all have to be selected though? Won't this highlight them all?
 
I'm not sure I follow what your trying to do here.

You have two select boxes, the first is filled with all the values from the recordset. When a user double clicks on one of these accounts it is added to the second listbox. Is the second listbox the one your interested in getting the values back from? If this is the case you will need to add a piece of script to your submit function that will select all the options in the second listbox before you submit:
Code:
var i, s;
s = frmAggregate.lstSubAccounts.options;
for(i = 0;i < s.length;i++){
  s[i].selected = true;
}
frmAggregate.submit();

Then, since all of the options will be selected before the submit, you will be able to split all of the options that were moved to the second listbox from the next page.

-Tarwn

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Do you know how hot your computer is running at home? I do
 
Yep, I have 2 list boxes, the user doubleclicks on the Client in the All Clients listbox, which then populates the Selected Clients listbox. I'm then doing some validation in javascript and submitting the form. I want to be able to insert the Clients in the Selected Clients listbox into the database! Does that make sense?
 
It works! Thanks, I'm still not sure why you can't just pass through the data in the listbox without it being selected though.

Cheers
Lbob
 
The way the listbox works is by presenting the user with some information and allowing them to make some selections. Since we generally know what is going in the listbox, the important part is what the user selectes. Everything else is overhead that just adds to the amount of data the user has to send back to us.

Since your using the listbox in another fashion, ie making it a holder for all selections the user made, we have to tweak it a little so that everything will be selected, and thus everything will be passed to the next page.

Perhaps one day someone will alter the way they work and give the developer a choice between only returning selections vs returning all of the items, but I doubt it. Consider this, if you receioved back all of the items of a listbox, selected and unselected, you would need some sort of extra set of form values just to tell which items in the select box were selected. So now your not only returning the full list, but also another list that lists all the selections. This basically will just be wasted communication in 99% of the cases where someone uses a listbox.

-Tarwn

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Do you know how hot your computer is running at home? I do
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top