ASP.NET Page_Unload to prevent user from leaving the page

Possible Duplicate:
Best way to detect when a user leaves a web page

I am trying to give the user the option to stay on the current page when they try to navigate away from it (assuming they haven't saved the changes in their progress). I'm not sure how to approach this as I tried and failed to execute JavaScript.

I hope there is a way inside the Page_Unload function in ASP.NET C # code.

any help would be much appreciated.

+2


source to share


6 answers


See this post "The best way to detect when a user leaves a page . " This should help you. You MUST do this on the client side, although as if they were leaving, the server never knows.



+4


source


You cannot rely on server-side code to do this. By the time the request arrives at the server, it is too late. You need to do this with javascript by providing an event handler for the beforeunload event. See the demo for a simple idea on how to do this.



+3


source


I would suggest JavaScript. For example, on SO you will be prompted if you try to exit the page after making any changes.

+1


source


You probably want to add a javascript function to the Body OnUnload event .

+1


source


I believe the best way is shown by tvanfosson above. You can inject this javascript server side by providing messages, redirects, etc.

0


source


This is a required parameter: the lifecycle of the Asp.NET page . Always keep in mind that only after the server side has finished running, the page is sent to the client.

-1


source







All Articles