Deleting Session Objects when user leaves a particular asp.net page. Unload? Dispose?
Posted on 2004-08-18
When a user visits one of my pages in my web application I create several objects and save them in session. These objects or only used for this particular page. The problem is, if a user leaves this page and comes back, the page loads the information from the session. What I want to do is delete those objects I created in session for this page when the user leaves. There are a couple of exit points for the page. I tried the following, but it seemed to fire the event with each post back (Dispose method).
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
public override void Dispose()
// DELETE SESSION OBJECT HERE <------------
// SEEMS TO FIRE WITH EACH POSTBACK
if(Session["dsSuggest"] != null)
private void InitializeComponent()
this.btnUpdateIdea.Click += new System.EventHandler(this.btnUpdateIdea_Click);
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
this.Load += new System.EventHandler(this.Page_Load);
Is there an event I can call that gets called only once when the user navigates to another page (will unload work?)? Please post example code.