0

When a page is loaded we use the page_load event to set a value to 1. in asp.net using C# i want to set a value to null whenever the user navigates away from that page. Page_unload does not help because it runs when the page is loaded itself. Which event should i use?

Seema
  • 107
  • 2
  • 5
  • 13

5 Answers5

3

Leaving a page does not require a hit to the server so there is no way to guarantee the execution of C# code when a user closes the browser for instance.

You can use javascript onbeforeunload to flash a warning message to the user. How much you can customize this message depends on the browser you are using (spoiler alert: FireFox won't let you customize this message). Obviously this would require that the user has javascript enabled.

You might be able to rig up some javascript that posts back to the server from the onbeforeunload event, but you will have to play with that to see if you can get it working for your purposes.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
1

If you're intent on doing it in C#, you could also try using Session_OnEnd event to set your variable.

ASP.NET provides two events that help you manage user sessions. The Session_OnStart event is raised when a new session starts, and the Session_OnEnd event is raised when a session is abandoned or expires. Session events are specified in the Global.asax file for an ASP.NET application.

EDIT Thanks to @jadarnel27's comment, I went and checked this out. Indeed, the session does not end when the user closes their browser. I found some excellent discussion here: Close/kill the session when the browser or tab is closed

Community
  • 1
  • 1
MichaelJCox
  • 756
  • 7
  • 17
  • 1
    The Session doesn't end upon navigating away from a page. Perhaps you could configure the Session that way (I don't know), but that seems like it defeats the purpose of the Session (which is to persist information across postbacks). – Josh Darnell May 09 '13 at 16:32
  • You're right @jadarnel27. +1 for causing me to advance my understanding of sessions. – MichaelJCox May 09 '13 at 16:49
0

If you look at the overall structure, you will realize that there can be no possible server side event for page unload, because, when you request a page, it is loaded in to memory, and all the controls are rendered.

But, when you click a link inside a page, then only a redirect is done, and it is done at the client end. So the server has no knowledge of it. It just knows now that a new page is requested. As the previous page is already unloaded from memory

So the solution must be client side.
Or,if you still want a completely server side solution, then look for session storage to store what page was loaded. Then, in the page_load event of the next page, you can check whether the last page loaded was this, or is this a new page. Though this doesn't answer your question, as you will get a page unload event only when a new page is loaded.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
0

Your best bet is to fire an Ajax query from client side when the user closes the page. There is no guarantee however that it will actually fire.

magnattic
  • 12,638
  • 13
  • 62
  • 115
0

It sounds like you are trying to set a variable on that page. Once you navigate away and come back, page_load will fire again and your variable will be set to 1 again. As it was said, "there is no way to guarantee the execution of C# code when a user closes the browser for instance."

If there was something in the database or whatever that you need to change you could just put it in the click event of the button doing the redirect. If there was a value that you need persisted you could put it in Session and update it before the redirect (though there my be better solutions than Session depending on what you are trying to accomplish.

protected void Button1_Click(object sender, EventArgs e)
{
    //change your value here
    Response.Redirect("MyPage");
}
user2315985
  • 2,848
  • 5
  • 17
  • 18