how to go from VB in a page to ASP in another page and come back

A

Andre

Hi,


This is 'test.htm'
---------------

<form name=totd>
<input name="ud" type="hidden" value="" >
</form>

sub dag_onchange()
'dag is a Select
dat=dag.value
if dat<>"99" then
'pass that value to ASP file via a form
document.getElementById("ud").value=dat
ins.action="test2.asp"
ins.method="post"
ins.submit

'i need the value totd generated by 'test2.asp' and then come back and
continue here ...
if totd=0 then.
......
end if
.........
end sub


table 'test2.asp'
--------------
<%
dat=request.form("ud")
set objdc = Server.CreateObject("ADODB.Connection")
objdc.Open("provider=Microsoft.Jet.OLEDB.4.0; Data Source
=c:\access\test.mdb")
sql = "select lcount(uur) as totdag from studres
set rs=Server.CreateObject("ADODB.recordset")
rs.open sql, objdc, 3, 3
rec=rs.recordcount
if rec > 0 then
totd=rs.Fields("totdag").Value
end if
%>

Now i would like to go back to 'test.asp' with the value of totd, and even
in the sub dag_change() procedure
..
Thanks for help
André
 
J

J. Alan Rueckgauer

Andre said:
Hi,


This is 'test.htm'
---------------

<form name=totd>
<input name="ud" type="hidden" value="" >
</form>

sub dag_onchange()
'dag is a Select
dat=dag.value
if dat<>"99" then
'pass that value to ASP file via a form
document.getElementById("ud").value=dat
ins.action="test2.asp"
ins.method="post"
ins.submit

'i need the value totd generated by 'test2.asp' and then come back and
continue here ...
if totd=0 then.
.....
end if
........
end sub


table 'test2.asp'
--------------
<%
dat=request.form("ud")
set objdc = Server.CreateObject("ADODB.Connection")
objdc.Open("provider=Microsoft.Jet.OLEDB.4.0; Data Source
=c:\access\test.mdb")
sql = "select lcount(uur) as totdag from studres
set rs=Server.CreateObject("ADODB.recordset")
rs.open sql, objdc, 3, 3
rec=rs.recordcount
if rec > 0 then
totd=rs.Fields("totdag").Value
end if
%>

Now i would like to go back to 'test.asp' with the value of totd, and even
in the sub dag_change() procedure
.
Thanks for help
André

Andre --

You can't call server-side script (as in an ASP script) from client-side
event code without a third-party tool. You have to either post your form or
get the desired script with a querystring for the server to know you want it
to do something.

You could do something like this in just one ASP:

** file test.asp **
<% option explicit %>
<%
dim strMethod, cn, strSQL, rs, dat, totd, IsPostback

' initialize variables so their purpose is clear
' and you don't have problems later if they're not initialized
IsPostback = False
strMethod = ""
dat = ""
totd = CLng(0) ' since totd holds a long later on
' also guarantees totd has a long integer value even if it isn't
changed

with Request
strMethod = LCase(.servervariables("REQUEST_METHOD"))
if strMethod = "post" then
IsPostback = True
' Assuming you're using IIS 5 or later, you can use
Server.Execute to call another script.
' However, for this example it'll be done in-line in this
script.
dat = .form("ud") ' you don't indicate just what 'dat' is used
for
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
strSQL = "select count(uur) as totdag from studres" ' lcount??
cn.Open("provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\access\test.mdb")
with rs
.cursortype = adOpenStatic ' 3
.locktype = adLockReadOnly ' 1
.activeconnection = cn
.open strSQL
.activeconnection = nothing
' if you are only reading, always close the connection
immediately
' only keep it open if you will be doing more work right
then
cn.close
if .recordcount > 0 then
' no need to assign recordcount unless you will use it
more than once
totd = CLng(.Fields("totdag").Value) ' casting numerics
is a good idea
end if
.close
end with
set rs = nothing
set cn = nothing
end if
end with
%>
<html>
<head>
</head>
<body>
<form <form name="totd" method="post">
<input name="ud" type="hidden" value="">
<% if 1 = 0 then %>
comment: you can use the dropdown's change event as you had
to do the assignment and submit, but you and just as easily get
its value as .form("thedropdownsname") from the script's
request object
<% end if %>
</form>
<% if IsPostback then %><br>The value of 'totd' is:&nbsp;<%=totd%><br><%
end if %>
</body>
</html>

Alternatively, you could put your processing code in a Function within the
script, or in another file containing common routines you use and use an
#include directive in pages you want it.

Alan
 
A

Andre

thanks ...

J. Alan Rueckgauer said:
Andre --

You can't call server-side script (as in an ASP script) from client-side
event code without a third-party tool. You have to either post your form or
get the desired script with a querystring for the server to know you want it
to do something.

You could do something like this in just one ASP:

** file test.asp **
<% option explicit %>
<%
dim strMethod, cn, strSQL, rs, dat, totd, IsPostback

' initialize variables so their purpose is clear
' and you don't have problems later if they're not initialized
IsPostback = False
strMethod = ""
dat = ""
totd = CLng(0) ' since totd holds a long later on
' also guarantees totd has a long integer value even if it isn't
changed

with Request
strMethod = LCase(.servervariables("REQUEST_METHOD"))
if strMethod = "post" then
IsPostback = True
' Assuming you're using IIS 5 or later, you can use
Server.Execute to call another script.
' However, for this example it'll be done in-line in this
script.
dat = .form("ud") ' you don't indicate just what 'dat' is used
for
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
strSQL = "select count(uur) as totdag from studres" ' lcount??
cn.Open("provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\access\test.mdb")
with rs
.cursortype = adOpenStatic ' 3
.locktype = adLockReadOnly ' 1
.activeconnection = cn
.open strSQL
.activeconnection = nothing
' if you are only reading, always close the connection
immediately
' only keep it open if you will be doing more work right
then
cn.close
if .recordcount > 0 then
' no need to assign recordcount unless you will use it
more than once
totd = CLng(.Fields("totdag").Value) ' casting numerics
is a good idea
end if
.close
end with
set rs = nothing
set cn = nothing
end if
end with
%>
<html>
<head>
</head>
<body>
<form <form name="totd" method="post">
<input name="ud" type="hidden" value="">
<% if 1 = 0 then %>
comment: you can use the dropdown's change event as you had
to do the assignment and submit, but you and just as easily get
its value as .form("thedropdownsname") from the script's
request object
<% end if %>
</form>
<% if IsPostback then %><br>The value of 'totd'
 
A

Andre

but if i understand your code, the value from ASP is put outside the SUB. Is
it not possible to get that value within the SUB dag_onchange(), because
that value must be tested in the SUB and determines the visibility style of
another SELECT in the same page etc ...
If i get that value outside SUB dag_onchange, how can i make the other
select visible?

thanks again
 
J

J. Alan Rueckgauer

Andre said:
but if i understand your code, the value from ASP is put outside the SUB. Is
it not possible to get that value within the SUB dag_onchange(), because
that value must be tested in the SUB and determines the visibility style of
another SELECT in the same page etc ...
If i get that value outside SUB dag_onchange, how can i make the other
select visible?

thanks again
[snip]

The way you have described it, "Sub dag_onchange" is client-side event
script, meaning it is executed in the browser, not the server. The server
has absolutely no knowledge of events that occur in the browser. It only
knows something is going on when the form is submitted back to the server.
The server can only leave results for the client through <%=...%> script
tags that are replaced when the server prepares the page, not after it is
received in the browser. For instance, if you have this in a client event
script block:

If dag.value = "<%=AServerSideVariable%>" then
'do something
End If

the result you'll see if you view the source in the browser would be
something like:

If dag.value = "This is what the server left." then

You can't do a direct comparison such as:

If dag.value = AServerSideVariable then

In other words, in the client script you are not comparing your variable
against a server variable, you are comparing the client variable to a
literal representing an actual *value* left by the server. From the client
perspective, it is the same as if you typed the value in yourself.

Try this sample to get an idea of what I mean (the code's formatted a bit
awkwardly, but that's to avoid line-wrap problems):

**** begin sample file "testforandre.asp" ****

<% option explicit %>
<%
dim IsPostback, strMethod, TextOptionPickVal
dim TextForOptionPick, TextForTextbox
dim MagicWord, UsedMagicWord

IsPostback = false
strMethod = ""
TextForOptionPick = ""
TextForTextbox = ""
MagicWord = "boo"
UsedMagicWord = false

with request
strMethod = lcase(.servervariables("REQUEST_METHOD"))
if strMethod = "post" then
IsPostback = true
TextOptionPickVal = .form("D1")
select case TextOptionPickVal
case ""
TextForOptionPick = "Nothing"
case "1"
TextForOptionPick = "the first item"
case "2"
TextForOptionPick = "the second item"
case "3"
TextForOptionPick = "the third item"
end select
TextForTextbox = trim(.Form("T1"))
If TextForTextbox = "" then
TextForTextbox = "<b>You didn't type anything.</b>"
Else
if instr(TextForTextbox, MagicWord) > 0 then
UsedMagicWord = true
end if
TextForTextbox = "You typed: " & _
"<font color=""red""><b>" & _
TextForTextbox & "</b></font>"
end if
end if
end with
%>
<html>
<head>
<title>Test Posting Script for Andre</title>
</head>
<body>
<form method="POST">
<p>Hello, Andre. Postback is <%=IsPostback%>.</p>

<% if IsPostBack then 'form was submitted %>
<p>You picked <%=TextForOptionPick%>
from the list (<%=TextOptionPickVal%>)</p>
<p><%=TextForTextbox%></p>
<p><% if UsedMagicWord then %>
You typed the magic word!
<% else %>
You did not type the magic word.
<% end if %></p>
<p>Is it making more sense now?</p>
<p>(use 'back' to try it again)</p>

<% else 'form wasn't submitted %>

<p>Fill out the form and click submit.</p>
<p>Pick one of these:
<select size="1" name="D1">
<option value="">(Please pick)</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">A Third Option</option>
</select></p>
<p>Type something here:
<input type="text" name="T1" size="20"></p>
<% if not UsedMagicWord then %>
(Try typing "<%=MagicWord%>" anywhere in the
textbox and see what happens.)
<% end if %>
<p>
<input type="submit" value="Submit" name="B1"></p>

<% end if %>
</form>
</body>
</html>

*** end of "testforandre.asp" ****

Run the page. In the browser, view | source both before and after you
submit it and compare it to the script code. Try it a number of times with
different inputs. You'll be able to see where the server variables were
inserted, and how the logic in the client script determined what to display
based on what you entered or whether you had submitted the form.

While I did not show you actual interaction between client event script and
the server, the principles are essentially the same: The server can work
with values you submit via form input controls, and you get back literal
values from the server that you can test in the client's script. However,
you can't directly mix the two.

I hope this clears it up a bit more.

Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top