web app page_unload

  • Thread starter Thread starter James Hawthorne
  • Start date Start date
J

James Hawthorne

Hi, I have created a asp.net web application and I hooked up page_unload so
I can clean up some things when i close it down.
The problem is, page_unload gets called when I first load the page, not when
I close it.
I know this because I put a breakpoint within the method, and it stops when
i first run it, not when I close.

private void Page_Unload(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("closing");
}

private void InitializeComponent()
{
this.btnAdopt.Click += new System.EventHandler(this.btnAdopt_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.Unload += new System.EventHandler(this.Page_Unload);
}

Any Ideas?
Thanks.
 
James:

The Load and Unload events both fire during the browser request to the server.
In other words, you point your browser to the URL for a web form, and when
the page is executing the Load and Unload events both fire at that time.
By the time the browser display the HTML the web form has finished the job
and there are no more events to fire. The server and the browser are disconnected.
 
James said:
Hi, I have created a asp.net web application and I hooked up page_unload so
I can clean up some things when i close it down.
The problem is, page_unload gets called when I first load the page, not when
I close it.
I know this because I put a breakpoint within the method, and it stops when
i first run it, not when I close.
The Unload event is raised when ASP.NET unloads the page from memory.
This happens every time the page is requested. To handle the client side
window.close event you'll have to use client side script.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Hi, I have created a asp.net web application and I hooked up page_unload so
I can clean up some things when i close it down.
The problem is, page_unload gets called when I first load the page, not when
I close it.
I know this because I put a breakpoint within the method, and it stops when
i first run it, not when I close.

private void Page_Unload(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("closing");
}

private void InitializeComponent()
{
this.btnAdopt.Click += new System.EventHandler(this.btnAdopt_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.Unload += new System.EventHandler(this.Page_Unload);
}

Any Ideas?
Thanks.

The page Unload event fires when the page object on the server is unloaded.
This will almost never correspond with when you close the page in your
browser window. The page object is created on the server when the page is
requested. Once the page object has processed everything and the HTML
stream has been sent to the browser, the server is basically then done with
the page object and it is unloaded. You can then hold the browser window
open as long as you like, but since that is just on the client machine
there are no server resources needed.
 
Back
Top