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!

Passing a JavaScript variable to be used in ASP

Status
Not open for further replies.

tseh

Programmer
Jan 13, 2000
64
CA
I have the following code for a dynamic linked list but am stuck. How do I make varCategory = selectedItem?

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function fillOptions(selectedItem){
<% Dim varCategory
varCategory = &quot;???&quot;
rs.MoveFirst
Do While Not rs.EOF
If (rs(&quot;category_&quot;) = varCategory) Then
%>
document.selectedOption.optionList.options[document.selectedOption.optionList.options.length] = new
Option(&quot;<% Response.Write(rs(&quot;option_id_&quot;) & &quot; &quot; & rs&quot;name_&quot;))%>&quot;, &quot;<% Response.Write(rs(&quot;option_id_&quot;)) %>&quot;);

<% End IF
rs.movenext
LOOP
%>
}
</SCRIPT>

Thanks,
Howard
Thanks,
Howard
 
Remember that selectedItem is not available at the time the ASP is being processed. You could do this as a two-stage process: load all of the options into an array in a function that is built dynamically in ASP. Then have a client-side only function that uses the array to fill the option list according to selectedItem.
Basically, the ASP (server-side) can't respond to user input (client-side javascript) without a return trip to the server.
 
How do I load an js array from an asp recordset?
 
var arr = new Array();
function loadArray() {
<% dim i
rs.MoveFirst
Do While Not rs.EOF
Response.Write &quot;arr[&quot; & i & &quot;] = '&quot; & rs(&quot;field&quot;) & &quot;';&quot;
Response.Write vbCrLf
i = i + 1
rs.MoveNext
Wend %>
}

Something like that.
 
For some reason, the square brackets didn't show up in my post... I'll retype it using curly braces, but they need to be changed to square brackets.
Response.Write &quot;arr{&quot; & i & &quot;} = '&quot; & rs(&quot;field&quot;) & &quot;';&quot;
 
use the
Code:
tags to encapsulate your code and they should be formatted as written

_____________________________________________________________________
Please help! I'm falling asleep over here!
onpnt2.gif

 
Rather than going through your entire, one-field recordset to create an array... try:

Code:
<%
' --- I assume you've already created recordset &quot;rs&quot; by now
dim rsArray, rsString
rsArray = rs.getRows
rsString = join(rsArray,&quot;|&quot;)
rs.Close : set rs = nothing     ' close recordsets
conn.Close : set Conn = nothing ' close connection
%>
...at this point you've now got a long string of the record values, separated by a vertical-bar...

So, now to get it into javascript...
Code:
<script type=&quot;text/javascript&quot; language=&quot;javascript&quot;>
var jsArray= '<%=rsString%>'.split('|');
</script>
...and now your Array is in javascript.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top