Click here to Skip to main content
15,868,127 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i have a test.aspx, test.aspx.cs pages..

in test.aspx.cs page, i have a function(save()) which serves saving the values of the test.aspx page.

when i am closing test.aspx page. i need to call the save(). how i can proceed??

please give a example when proving solution..


i tried with page_UnLOad() event but nolt working....
Posted
Updated 2-Nov-11 1:55am
v2
Comments
DaveAuld 2-Nov-11 8:03am    
Depends how you are closing the page? Are you just closing the browser or clicking a button? This may tell us why it does not work.

<pre lang="cs">function OnBeforeUnLoad () {
return &quot;All data that you have entered will be lost!&quot;;
// or you can make ajax call here
}</pre>



<body onbeforeunload="return OnBeforeUnLoad()">
 
Share this answer
 
Comments
CHill60 13-Jan-15 5:26am    
Please do not post solutions to old questions that already have an accepted answer - you have not added anything to the other 6 solutions
Bear in mind that you can never guarantee to get notification of a page close. Even if you have watertight Javascript, the browser process can crash or the connection can be lost.

If you want the information entered on one page to be passed to the next, you can use ordinary HTTP methods (putting it in the URL for a GET request or making a POST submission). If you want information to be saved without the use of a Save or Submit button, you should be saving it each time it changes, using client-side JavaScript and AJAX. Trying to do anything on page close is fragile and should be avoided.
 
Share this answer
 
Depending on how you are closing will determine how you need to handle the situation.

Have a read of the page lifecycle; http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events[^]

You need to look where in lifecycle you want to call this Save().
 
Share this answer
 
you catch the Event when you close your browser the best way you do this on client side like this.

ASP.NET
<body id="bdycnts"  runat="server"  önunload="fnthis()"></body>
 
Share this answer
 
v2
Comments
Kartheek.chakrala 2-Nov-11 8:32am    
yes. me done exact but with it the function not firing
if you close the browser, no events are executed...

kindly mind it.. the events are executed at server end... not at client end.. if you close browser, server will not know anything...

the unload event will be executed at the time of first time serving the page... and while postback of the page... (events are executed at the time of serving a requested page.. all the events are executed before you see the actual output)

use some javascript to stop the user to close the browser.. like "Do you really want to move out of the page, your data is not saved!!!" or use ajax to save while user is entering the data.
 
Share this answer
 
Comments
Kartheek.chakrala 2-Nov-11 8:10am    
yes. i have a C# function to be execute while browser window closing....

so, how can i with java script.
How do you close it? If you close browser tab, I think, there is no way to do it using asp.net (this topic[^] proves my word).

But you can try to write some Javascript. For this purpose read this[^] or try to google for another methods.
 
Share this answer
 
Comments
Kartheek.chakrala 2-Nov-11 8:09am    
there are so many options to escape from page
Ex:
1) i can escape by click on menu item
2) i can click tab close
3) i can click short cut keys
4) i can close browser window


may many things happen....

but in all cases end user data will save in database..
Nickos_me 2-Nov-11 8:27am    
I think, it isn't real to handle all of cases. But Solution 1 is pretty good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900