VBScript globals in event handler block? 
Author Message
 VBScript globals in event handler block?

I don't understand why I'm having trouble with a global variable going out
of scope.  After staring at it for more than a day I'd sure appreciate any
help...

I start my page with

<%Option explicit%>
<%Public intParent, xyz%>

In my main code block, marked by <% %> signs, I have several subs, one of
which is an event handler for a DTC button.  Here's a fragment:

'*****************************************************************
sub fetch_detail(docID)
 strQuery = " WHERE (CommentID = " + CStr(docID) + ");"
 Response.Write("<BR>In fetch sub, intParent is " + CStr(intParent) +
"<BR>")
 Response.Write("<BR>In fetch sub, xyz is " + CStr(xyz) + "<BR>")
 call RunQuery(strQuery)
end sub
'*****************************************************************
Sub btnPost_onclick()
Response.Write("<BR>intParent in onclick is " + CStr(intParent) + "<BR>")
Response.Write("<BR>xyz in onclick is " + CStr(xyz) + "<BR>")
...more stuff
'*****************************************************************

The variables have their value set initially at the top of the page.  The
writes in the first sub show correct values.  The writes in the onclick show
a blank, and the de{*filter*} says the variables are empty.  Variable xyz is
used in no other place - I just put it in to make sure I wasn't
inadvertently screwing up the intParent variable somewhere else.  This is
driving me crazy.  I need to be able to use the script level (global)
variables in the event handlers.  Can you see what I'm doing wrong?

Thanks,

Robin



Sun, 17 Jun 2001 03:00:00 GMT  
 VBScript globals in event handler block?
You're confusing server-side with client-side scripts.

The global variables are defined in the server-side script.  Your event
handler are (must be) in your client-side script.  IOW, your event handlers
must be it <SCRIPT Language=VBScript> ... </SCRIPT> tags, not <% %>.

You can set their values in your client-side scripts with substitution:

Sub btnPost_onclick()
  Response.Write("<BR>intParent in onclick is <%= intParent %><BR>")
  Response.Write("<BR>xyz in onclick is <%=xyz %><BR>")
  ...more stuff

(I doubt that you want Response.Write in you onClick handler, though...)

Ken

Quote:

>I don't understand why I'm having trouble with a global variable going out
>of scope.  After staring at it for more than a day I'd sure appreciate any
>help...

>I start my page with

><%Option explicit%>
><%Public intParent, xyz%>

>In my main code block, marked by <% %> signs, I have several subs, one of
>which is an event handler for a DTC button.  Here's a fragment:

>'*****************************************************************
>sub fetch_detail(docID)
> strQuery = " WHERE (CommentID = " + CStr(docID) + ");"
> Response.Write("<BR>In fetch sub, intParent is " + CStr(intParent) +
>"<BR>")
> Response.Write("<BR>In fetch sub, xyz is " + CStr(xyz) + "<BR>")
> call RunQuery(strQuery)
>end sub
>'*****************************************************************
>Sub btnPost_onclick()
>Response.Write("<BR>intParent in onclick is " + CStr(intParent) + "<BR>")
>Response.Write("<BR>xyz in onclick is " + CStr(xyz) + "<BR>")
>...more stuff
>'*****************************************************************

>The variables have their value set initially at the top of the page.  The
>writes in the first sub show correct values.  The writes in the onclick show
>a blank, and the de{*filter*} says the variables are empty.  Variable xyz is
>used in no other place - I just put it in to make sure I wasn't
>inadvertently screwing up the intParent variable somewhere else.  This is
>driving me crazy.  I need to be able to use the script level (global)
>variables in the event handlers.  Can you see what I'm doing wrong?

>Thanks,

>Robin



Mon, 18 Jun 2001 03:00:00 GMT  
 VBScript globals in event handler block?
Ken,

I am indeed confused, although (with your help) I'm starting to see this.
If I use a regular HTML button and double click the onclick event in the
script outline window, it generates a client side JavaScript block for me.
If I use a DTC button, the generated onclick is a server side vbscript
block.  I see your point about the event handler being logically on the
client side but does this mean that with the DTC's every event goes straight
to the server.  Looks like it does.  (Of course, assuming it does I still
don't see why my global was out of scope in my server side code...)  Still a
bit confused...
By the way, I'm avoiding any client side vb as many of my users are on
netscape.

Thanks,

Robin


Quote:
>You're confusing server-side with client-side scripts.

>The global variables are defined in the server-side script.  Your event
>handler are (must be) in your client-side script.  IOW, your event handlers
>must be it <SCRIPT Language=VBScript> ... </SCRIPT> tags, not <% %>.



Mon, 18 Jun 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Multithreading in Com Add in blocks Outlook Event Handlers

2. Add an event handler to the actual event handler for an element

3. object created in workbook_open event handler disappears on exit from the event handler

4. VBScript event handlers

5. Help on event handlers and VBScript

6. Event Handlers in VBScript

7. Event Handler in VBScript

8. Can u do Event Handlers w/vbScript?

9. Setting VBScript event handler at run-time

10. Returning Numeric Value From VBScript Event Handler

11. JScript event handler for ActiveX DLL events within IE

12. Determining which object fired event from event handler.

 

 
Powered by phpBB® Forum Software