Apparently I am not familiar with the Life Cycle of a page in ASP.NET. This became apparent when I wanted to dispose of a Session variable after I left the page. I did what made the most sense:
protected void Page_Unload(object sender, EventArgs e)
{
Session.Remove("ServiceSearch");
}
What I didn't know is that this would be called when I go from AND to the page. What I am wanting to do is dispose of that Session variable whenever the user leaves the page. How do I do this?
Page_Unload
refers to unloading thePage
object right before it is disposed after parsing and creating the page. It has nothing to do with leaving the page. Like @Nick says, there is really no good way to tell that, except to control every exit path. And you can't, because you can't control when the user hits back, or goes to google.com and then pastes in the url they were just at into the browser, etc.If you want to remove the
Session
variable just so it doesn't get re-used unintentionally, a better solution is to overwrite theSession
variable every time you enter the page, and just let it be disposed with the session on its own time when the session expires.