Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Capture "unload" of form

Capture "unload" of form

Scheduled Pinned Locked Moved ASP.NET
helpjavascripttutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Charles I Dont Care About The Back End Sustek
    wrote on last edited by
    #1

    I need to be able to capture an event that triggers when a user navigates away from the form they're currently using. When they do, I need to be able to take the contents of the controls on the page and save them in Session, so that when they return they see the data they just entered right where they left it. I think the first part is using the window.onUnLoad javascript event. After that, I'm unsure how to proceed. Am I completely on crack here? I am sure that there are at least 7.3 other solutions to this problem. Help!

    M 1 Reply Last reply
    0
    • C Charles I Dont Care About The Back End Sustek

      I need to be able to capture an event that triggers when a user navigates away from the form they're currently using. When they do, I need to be able to take the contents of the controls on the page and save them in Session, so that when they return they see the data they just entered right where they left it. I think the first part is using the window.onUnLoad javascript event. After that, I'm unsure how to proceed. Am I completely on crack here? I am sure that there are at least 7.3 other solutions to this problem. Help!

      M Offline
      M Offline
      Marcie Jones
      wrote on last edited by
      #2

      Hi Charles!! This is a tricky question. You can use the window.onunload, but that happens on the client-side and doesn't necessarily get you what you need back on the server-side. If it were me, and I'd half-way filled out a form, then navigated away, I wouldn't expect to come back to that page and my data be as I'd left it, halfway typed in. I'd expect to be SOL and retype everything, but that's just me. Marcie "I used to work with Charles" Robillard http://www.codeproject.com

      C 1 Reply Last reply
      0
      • M Marcie Jones

        Hi Charles!! This is a tricky question. You can use the window.onunload, but that happens on the client-side and doesn't necessarily get you what you need back on the server-side. If it were me, and I'd half-way filled out a form, then navigated away, I wouldn't expect to come back to that page and my data be as I'd left it, halfway typed in. I'd expect to be SOL and retype everything, but that's just me. Marcie "I used to work with Charles" Robillard http://www.codeproject.com

        C Offline
        C Offline
        Charles I Dont Care About The Back End Sustek
        wrote on last edited by
        #3

        Hey Marcie! Jerry and I are stuck. The situation here continues, although we hope for a solution in the coming months. We have been basically ordered to make our applications (dues, meeting registration) retain the information if the user clicks away. It was quoted as "unacceptable" if the user went halfway through the form, clicked away, and then came back and didn't see their changes right where they had left them. Is there no way to trigger some sort of event back to the server in this case? If there is not, that's fine. As long as we can prove why it can't be done, then hopefully we can inform the powers that be of that fact. ___________________________________________________________________ Ultimate wisdom from the WebMaster: "You know what...I just don't like that."

        J 1 Reply Last reply
        0
        • C Charles I Dont Care About The Back End Sustek

          Hey Marcie! Jerry and I are stuck. The situation here continues, although we hope for a solution in the coming months. We have been basically ordered to make our applications (dues, meeting registration) retain the information if the user clicks away. It was quoted as "unacceptable" if the user went halfway through the form, clicked away, and then came back and didn't see their changes right where they had left them. Is there no way to trigger some sort of event back to the server in this case? If there is not, that's fine. As long as we can prove why it can't be done, then hopefully we can inform the powers that be of that fact. ___________________________________________________________________ Ultimate wisdom from the WebMaster: "You know what...I just don't like that."

          J Offline
          J Offline
          Jesse Squire
          wrote on last edited by
          #4

          Wow... this one sure is interesting. Here's the convoluted ramblings that come to my mind... hopefully I can pound them to the point where they make some kind of sense. :) Create a custom control that implements IPostBackEventHandler. When rendered, the control attaches an event, using javascript, to the window.unload event. The unload handler will change the target attribute of the runat = "server" form to '_blank', and force a postback event for the control. The server side event handler will scrape and save the data, and render a startup script to close the new window. The idea is that the control will force the data to be posted and saved in a new window, to minimize the penalty of disturbing the user. The drawback is that they will see the flashing of a new window. You could always skip closing the new window and display a message about data being saved for the future. One catch that I just wanted to mention. Although it is possible to extend the System.Web.UI.Page class and implement the IPostBackEventHandler interface, in my experience the interface method doesn't get called properly. That is why I suggested creating a control, instead. While, regretably, I don't have time to run down the concept in full, I've been successfully using the same basic technique [of hijacking the target property] to force an Excel export in a new window without disturbing the user's current task. Here's a quick sketch of the control class:

          public class  UnloadMonitor : System.Web.UI.Control, IPostBackEventHandler
          {
            private bool _UnloadInProgress = false;
           
            protected override void OnPreRender(System.EventArgs ea)
            {
              if(!_UnloadInProgress)
              {
                 Page.RegisterStartupScript("" + 
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"window.onunload=\"var temp = document.forms[0].target; document.forms[0].target='_blank'; " +<br>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n</x-turndown>
          
          M 1 Reply Last reply
          0
          • J Jesse Squire

            Wow... this one sure is interesting. Here's the convoluted ramblings that come to my mind... hopefully I can pound them to the point where they make some kind of sense. :) Create a custom control that implements IPostBackEventHandler. When rendered, the control attaches an event, using javascript, to the window.unload event. The unload handler will change the target attribute of the runat = "server" form to '_blank', and force a postback event for the control. The server side event handler will scrape and save the data, and render a startup script to close the new window. The idea is that the control will force the data to be posted and saved in a new window, to minimize the penalty of disturbing the user. The drawback is that they will see the flashing of a new window. You could always skip closing the new window and display a message about data being saved for the future. One catch that I just wanted to mention. Although it is possible to extend the System.Web.UI.Page class and implement the IPostBackEventHandler interface, in my experience the interface method doesn't get called properly. That is why I suggested creating a control, instead. While, regretably, I don't have time to run down the concept in full, I've been successfully using the same basic technique [of hijacking the target property] to force an Excel export in a new window without disturbing the user's current task. Here's a quick sketch of the control class:

            public class  UnloadMonitor : System.Web.UI.Control, IPostBackEventHandler
            {
              private bool _UnloadInProgress = false;
             
              protected override void OnPreRender(System.EventArgs ea)
              {
                if(!_UnloadInProgress)
                {
                   Page.RegisterStartupScript("" + 
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"window.onunload=\"var temp = document.forms[0].target; document.forms[0].target='_blank'; " +<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n</x-turndown>
            
            M Offline
            M Offline
            Marcie Jones
            wrote on last edited by
            #5

            There you have it Charles, this oughtta do the trick and keep Elaine happy (just guessing that she requested this feature). Thank goodness CP has smart people like Jesse, I wouldn't have dreamed up code like this in a million years :) Marcie http://www.codeproject.com

            J 1 Reply Last reply
            0
            • M Marcie Jones

              There you have it Charles, this oughtta do the trick and keep Elaine happy (just guessing that she requested this feature). Thank goodness CP has smart people like Jesse, I wouldn't have dreamed up code like this in a million years :) Marcie http://www.codeproject.com

              J Offline
              J Offline
              Jesse Squire
              wrote on last edited by
              #6

              Marcie Robillard (Datagrid Girl) wrote: smart people like Jesse I prefer sick and twisted, personally... but thanks very much for the compliment. :-D   --Jesse

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups