Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / September 2003

Tip: Looking for answers? Try searching our database.

onbeforeunload

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Peacock - 08 Sep 2003 17:42 GMT
To prevent accidental loss of lengthy text entry when a
user abruptly closes or navigates away from a page, we're
using

<body onbeforeunload="SaveTextEntry()">

to submit the form and execute server code before
unloading. In early tests we haven't been able to make it
fail; but is this the best way to accomplish this?

Thanks.
MP
Lewis Wang [MSFT] - 09 Sep 2003 03:56 GMT
Hi Mark,

I think it's not a bad idea to submit the form and execute server code by
"onbeforeunload". It works OK on my side, too.

If you need more information, just post it here, thanks.

Best regards,
Lewis
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Mark Peacock" <markp@fb.com>
[quoted text clipped - 13 lines]
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.caching:1397
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.caching
[quoted text clipped - 11 lines]
| Thanks.
| MP
Mark Peacock - 09 Sep 2003 14:38 GMT
Lewis,

thanks for your prompt response. The question has become
more complicated by more realistic tests (over the
production WAN) in which onbeforeunload fails
intermittently.

It seems as if client and server events only work
sequentially with each other by coincidence. So, if the
client event cycle of the page -- in this case, pre-
unloading, being re-submitted and finally unloading --
completes before the server-side code finishes, IE 5.5+
unloads and cuts short the code behind.

Is this an accurate understanding of what is happening?
How can we assure that server-side code that is fired by
onbeforeunload will complete? Or is there a better way to
accomplish this?

Thanks,
MP

>-----Original Message-----
>Hi Mark,
[quoted text clipped - 44 lines]
>
>.
Lewis Wang [MSFT] - 10 Sep 2003 09:24 GMT
Hi Mark,

I have done a simple test, but I can not reproduce this issue. If we submit
the web form in SaveTextEntry(), the server side will execute the code in
the Page_Load. Do you mean sometimes IE 5.5+ may cut short the code behind?
Would you please give me some steps to reproduce it?

You can write some logs at the end of Page_Load to confirm that server-side
code completes.

Hope this helps.

Best regards,
Lewis
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Mark Peacock" <markp@fb.com>
| Sender: "Mark Peacock" <markp@fb.com>
| References: <3bda01c37628$3c7202a0$a301280a@phx.gbl>
<8vf#T4ndDHA.1996@cpmsftngxa06.phx.gbl>
| Subject: RE: onbeforeunload
| Date: Tue, 9 Sep 2003 06:38:02 -0700
[quoted text clipped - 10 lines]
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.caching:1401
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.caching
[quoted text clipped - 75 lines]
| >
| >.
Mark Peacock - 10 Sep 2003 20:53 GMT
Lewis,

I appreciate your trying to simulate the problem. This
should help you see what we're seeing. (I sent you the
zipped solution by e-mail.) It uses Thread.Sleep in the
code behind to simulate server processing delay.

client script:

<HEAD><script language="vbscript">
Sub window_onbeforeunload()           
  Form1.btnSave.click       
End Sub   
</script></HEAD>
<body onbeforeunload="window_onbeforeunload()">
<form id="Form1" method="post" runat="server">

Code behind:

Private Sub Page_Load(..  
 With sb
  .Append("User action:") 'Exit browser etc.
  .Append("Event:") 'Page_Load
  .Append("Server delay:") '1-5000 ms
  .......      
 End With
 LogFile(sb.ToString)
End Sub

Private Sub btnSave_ServerClick(...
 LogFile("btnSave_ServerClick")
 Thread.Sleep('1-5000 ms
 LogFile("Finished server processing. ")
End Sub
 
The log output which apparently reveals the problem:
(According to other (unresolved) posts we've seen
elsewhere on this problem, a double postback is expected
with window_onbeforeunload; hence the duplicative log
entries.)

On the first run we see the complete, expected event cycle:
-The initial page load
-Subsequent page load fired by window_onbeforeunload
-Entering the btnSave_ServerClick event
-Finishing any server processing, after a 1 ms delay
(The server processing is on a fast intranet and likely
less than a second.)

-User action: Exit Browser; Event: Page_Load; Server
delay: 1 ms; 1:41:58 PM
-Event: btnSave_ServerClick; 1:41:58 PM
-Finished server processing. 1:41:58 PM
-User action: Exit Browser; Event: Page_Load; Server
delay: 1 ms; 1:41:58 PM
-Event: btnSave_ServerClick; 1:41:58 PM
-Finished server processing. 1:41:58 PM

On the 2nd run, even at 1 ms, none of the post-
onbeforeunload server processing completes.

-User action: Exit Browser; Event: Page_Load; Server
delay: 1 ms; 1:43:24 PM

A newsgroup tip we're using is to use <body
onbeforeunload="window_onbeforeunload()"> to call Sub
window_onbeforeunload() as this is thought to increase the
chance of getting the server code to run; which seems to
be confirmed (though not understood) in our tests.

The 3rd time we try it at 5000 ms, most of the
duplicative  cycle completes, even with an artificially
long delay. It's enough to process our server code (which
we expect may run twice, in best case).

-User action: Exit Browser; Event: Page_Load; Server
delay: 5000 ms; 1:43:32 PM
-Event: btnSave_ServerClick; 1:43:32 PM
Finished server processing. 1:43:37 PM
User action: Exit Browser; Event: Page_Load; Server delay:
5000 ms; 1:43:37 PM
Event: btnSave_ServerClick; 1:43:37 PM

2 more times when no server-side processing occurs, even
with only a 1 ms delay.

User action: Exit Browser; Event: Page_Load; Server delay:
5000  ms; 1:44:29 PM

User action: Exit Browser; Event: Page_Load; Server delay:
1 ms; 1:45:13 PM

Finally, 2 in a row that behave as expected. Definitely
intermittent!

User action: Exit Browser; Event: Page_Load; Server delay:
1 ms; 1:46:35 PM
Event: btnSave_ServerClick; 1:46:35 PM
Finished server processing. 1:46:35 PM
User action: Exit Browser; Event: Page_Load; Server delay:
1 ms; 1:46:36 PM
Event: btnSave_ServerClick; 1:46:36 PM
Finished server processing. 1:46:36 PM

User action: Exit Browser; Event: Page_Load; Server delay:
1 ms; 2:01:20 PM
Event: btnSave_ServerClick; 2:01:20 PM
Finished server processing. 2:01:20 PM
User action: Exit Browser; Event: Page_Load; Server delay:
1 ms; 2:01:20 PM
Event: btnSave_ServerClick; 2:01:20 PM
Finished server processing. 2:01:20 PM

I hope this clarifies the problem. From what we've seen in
the groups, we won't be the only developers who would love
to get a solution to this.

Thanks again.
MP

>-----Original Message-----
>Hi Mark,
[quoted text clipped - 114 lines]
>
>.
Lewis Wang [MSFT] - 11 Sep 2003 10:45 GMT
Hi Mark,

After my research, I found that the reason for the intermittent POSTS is
due to a form that gets submitted asynchronously. In other words, closing
IE won't wait for the event to finish Processing.

I found the following workarounds:

1. Submitting the form in a hidden frame

You can have a hidden frame which does the actual submit. The main page can
then ask the frame to post it in its unload handler. If user closes the
browser, then there is no way to do this. You can open a new window, do the
submit and close it.

2. Doing a window.open and submitting the form there.

3. Prolong the onbeforeunload by doing a loop or something similar.

Hope this helps.

Best regards,
Lewis
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Mark Peacock" <mark@fb.com>
| Sender: "Mark Peacock" <mark@fb.com>
| References: <3bda01c37628$3c7202a0$a301280a@phx.gbl>
<8vf#T4ndDHA.1996@cpmsftngxa06.phx.gbl>
<060001c376d7$97774c50$a001280a@phx.gbl>
<ytHprT3dDHA.2080@cpmsftngxa06.phx.gbl>
| Subject: RE: onbeforeunload
| Date: Wed, 10 Sep 2003 12:53:50 -0700
[quoted text clipped - 10 lines]
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.caching:1408
| NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.caching
[quoted text clipped - 252 lines]
| >
| >.
Mark Peacock - 13 Sep 2003 00:02 GMT
Lewis,

I guess the idea of browser application, integrating
client and server processing, is still a stretch; but your
suggestion to prolong onbeforeunload with a loop seems to
do what we need.

Reading a hidden field, updated by code-behind, from
within the loop in onbeforeunload didn't work. Client
script never seemed to refresh the hidden form value to
catch the update. But it did notice an updated cookie
(updated by code-behind) and kick out of the loop to
finish unload.

Relying on cookies isn't perfect, but it's OK on IE-only
intranet.

In case anyone (like me, who is not a client script
expert) can profit by somewhat-tested code, here it is:

<script lang="javascript">           
function GetCookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
    return unescape(aCrumb[1]);
   }
  return null;
}   
</script>
<script language="vbscript" id="clientEventHandlersVBS">
<!--
Sub btnShow_onclick
Dim sCookie
sCookie=GetCookieForm1.lstCookies.value)    
Form1.txtShow.value = sCookie           
End Sub

Sub btnSet_onclick
document.cookie = Form1.lstCookies.value & " = " &  
Form1.txtSet.value & "; expires=Fri, 31 Dec 2003 23:59:59
GMT"                   
End Sub
   
Sub window_onbeforeunload               
Form1.btnSave.click   
Do
  msgbox(GetCookie("Eval1"))
Loop Until GetCookie("Eval1") = "Done"    
End Sub
-->
</script></HEAD>

Code-behind:

Private Sub Page_Load(..
 Response.Cookies("Eval1").Value = "Start"    
End Sub

Private Sub btnSave_ServerClick(...
'  ... delay from server processing

Response.Cookies("Eval1").Value = "Done"

End Sub

Thanks for the help.
MP
>-----Original Message-----
>Hi Mark,
[quoted text clipped - 304 lines]
>
>.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.