Page_Unload Problem in ASP.Net 2.0

G

Guest

Hi,

After converting my projects to ASP.Net 2.0 I have seen a problem with
Page_unload. Ä°n framework 1.1 page_unload event fires when you are leaving
the page or has closed the browser window. Now in 2.0 when you initally load
the page Page_unload event fires after page_load event. In some of our aspx
pages we craete some versions of original records and if the user presses
cancel button or just leaves the page without cancelling we have to do some
cleaning and rollbacks in the database.

I have searched the net, but I couldn't have found a possible solution or a
workaround. Is there an event which just fires when the page is closing?

TIA

Cenk
 
K

Karl Seguin [MVP]

Cenk:
Page_Unload fires the same way. I'm 99.9999999% sure that in 1.1 it fired
the exact same way, after page_load. For it to work the way you say it did,
it would have to postback to the server. It is possible someone hooked a
postback into the unbeforeunload JAVASCRIPT method, which would then cause
your entire page to reprocess.

From:
http://www.sitepoint.com/article/asp-net-server-controls/6
(which was done in 2001 -clearly not for 2.0)
"One thing to note is that the unloading of a page doesn't happen when you
close the browser or move to another page. The Page_Unload event happens
when the page has finished being processed by ASP.NET, and before it's sent
to the browser."

The only thing that's aware of a browser closing is javascript - which you
could use to cause a postback and have server-side clean up take place.

Karl
 
G

Guest

Hi Karl,

Thanks for your reply. Maybe I was doing something wrong that worked in the
right way.

For the functionality I have mentioned I have two methods in my code which
is the following.

override protected void OnUnload(EventArgs e)
{
base.OnUnload(e);
this.Unload += new EventHandler(Page_Unload);
}

private void Page_Unload(object sender, System.EventArgs e)
{
Some code for clean up
}

I traced the code in v1.1 and has seen that when the page is initially
loaded the OnUnload method is called but Page_Unload doesn't. When I am
leaving the page both methods are called and they do what I want.

In v2.0 initially both methods are called which is different in the old
version and when I am leaving the page none of them are called again.

I want to mention this difference. Is there a way to prevent this?

It is a good idea to write javascript code and do some cleaning.

Thanks
 
K

Karl Seguin [MVP]

That is wrong ;)

There's no need to override the event and then hook into it again - just
extra work for nothing. Even so, the only way it would work is if you are
posting back when they leave. Like there's a linkbutton or a button that
says "Cancel" or "home" that actually does a postback and
Response.Redirects()..

if they simply close their browser, use their back button or following a
plain link, I still refuse to believe this worked without having additional
javascript in there :)

Karl
 
G

Guest

Well, it works in some way, I can send you an example project if you want.

In 2.0 it doesn't, I have to search for another way.
 
K

Karl Seguin [MVP]

I've taken a look at ur sample application. I can't tell you why it doesn't
work in 2.0 (i opened that project in 2.0 and the session was removed when
going back to webform1.aspx).

I'm still not sure you understand what's going on in either 1.1 or 2.0 with
respect to this.

Page_Unload fires after Render, but before the page is actually on the
browser

It goes
init
load
render
unload
--> send to browser.

Unload doesn't fire when the user hits "x" on their browser or anything like
that.

Look at ur example, you set a session in webform1, then go to webform2. When
webform2 first loads, the session will be stripped 'cuz unload will fire and
remove it - before the page is even loaded on the client, let alone when
they close their browser. So of course it's working, but I don't think it's
working how you think it is. The "cleanup" is happening much sooner than
you are thinking. It's very possible this is what you want...and again, I'd
exect it to work as-is in 2.0. In your example, unload actually fires
twice...once when the page is first loaded and again when the button is
clicked which causes a postback and the button event is fired.

Karl
 
G

Guest

Hi Cenk!

Did you find any way to do the Page_Unload equal in the 1.1 version ?
I had the same problem!

Thks
Riccardo
 
G

Guest

Hi,

There is no solution to this problem in 2.0. You need to change your pages
in a way that original record is not affected until the user hits the save
button. But if the users have a bad habit of leaving the page by selecting
another menu item or just by pressing the cross of the IE window there will
be lots of dummy records. There must be some process which deletes these
records

A lot of people responded to me that they are using page_unload for cleaning
up in 1.1 even though MS did not designed it for this purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top