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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set an asp variable in javascript

Status
Not open for further replies.

nmath

Programmer
Dec 12, 2003
47
US
I am using javascript and ASP together and I am having a hard time setting a variable. I want to set an ASP varible to a certain value inside of a javascript function. Here is my code:

function setMeOR() {
<%
selection = &quot;OR&quot;
%>
//do some error checking to see if I get in this function
if(1==1)
alert(&quot;I am in the or function&quot;);
}

I execute the function but the variable doesn't seem to get set. [santa]
 
you cannot do this: the asp executes server-side before the client-side js is loaded.

what are you trying to accomplish? your example code doesn't even attempt to use the &quot;selection&quot; variable


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
I haven't worked with ASP but I am a JSP/Java programmer so they should work the same way in this respect. When you load a page it doesn't run from top to bottom. It runs server-side code first, then JS, then HTML. So it's not possible to change the variable value once the page has loaded the ASP code. This is why you can set JS variables to the ASP/JSP variables with

var jsvariable = <%=aspvariable%>;

Because it runs the script after the ASP code is executed. You will have to reload the page passing the variable to the ASP with a request. Hope this helped!

CF
 
I have a page that has two tables. Depending on which button is clicked will determine which table is displayed. I am not really good w/ javascript so I was trying to use a variable and test it so I would know which table to display. I am open to any suggestions!! Here is the form code and the test that I use:

<FORM NAME = &quot;main&quot; ACTION=&quot;createquery.asp?&quot; METHOD=&quot;POST&quot;>
<%

if selection = &quot;AND&quot; then
%>
<input type=&quot;hidden&quot; id=&quot;selection&quot; name=&quot;selection&quot;>

<TABLE ALIGN=&quot;left&quot;>
<TR>
<TD>AND</TD>
<TD><INPUT TYPE=TEXT NAME=one MAXLENGTH=20></TD></TR>
<TR><TD>AND</TD>
<TD><INPUT TYPE=TEXT NAME=two MAXLENGTH=20></TD>
<TD><input type=button value=&quot;AND&quot; onclick=&quot;setMeAND();&quot;></TD>
<TD> <input type=button value=&quot;OR&quot; onclick=&quot;setMeOR();&quot;></TD></TR>
<TR><TD>AND</TD>
<TD><INPUT TYPE=TEXT NAME=three MAXLENGTH=20></TD></TR>
<TR><TD>AND</TD><TD>
<INPUT TYPE=TEXT NAME=four MAXLENGTH=20></TD></TR>
<TR><TD>AND</TD>
<TD><INPUT TYPE=TEXT NAME=five MAXLENGTH=20></TD><TD> </TD></TR>
</TABLE>
<% else %>
<TABLE ALIGN=&quot;left&quot;>
<TR><TD>OR</TD>
<TD><INPUT TYPE=TEXT NAME=one MAXLENGTH=20></TD><TD> 
</TD></TR>
<TR><TD>OR</TD>
<TD><INPUT TYPE=TEXT NAME=two MAXLENGTH=20></TD>
<TD>  <input type=button value=&quot;AND&quot; onclick=&quot;setMeAND();&quot;></TD>
<TD> <input type=button value=&quot;OR&quot; onclick=&quot;setMeOR();&quot;></TD></TR>
<TR><TD>OR</TD>
<TD><INPUT TYPE=TEXT NAME=three MAXLENGTH=20></TD>
<TD> </TD></TR>
<TR><TD>OR</TD>
<TD><INPUT TYPE=TEXT NAME=four MAXLENGTH=20></TD>
<TD> </TD></TR>
<TR><TD>OR</TD>
<TD><INPUT TYPE=TEXT NAME=five MAXLENGTH=20></TD>
<TD> </TD></TR>
</TABLE>
<% end if %>
</FORM>

Thanks! [santa]
 
The only way you are gonna do this with asp is by reloading the page. You can't dynamically set an asp variable. You can however, pass the variable to a reload of the page so that it displays the right table on reload. An alternative is to do it client side and use visible tags to display or hide the tables. These values can be changed dynamically and therefore prevents you from having to reload the page.

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top