By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Page_Unload Problem in ASP.Net 2.0

P: n/a
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
Mar 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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...ver-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

--
http://www.openmymind.net/

"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Hi,

After converting my projects to ASP.Net 2.0 I have seen a problem with
Page_unload. In 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

Mar 10 '06 #2

P: n/a
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

"Karl Seguin [MVP]" wrote:
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...ver-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

--
http://www.openmymind.net/

"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Hi,

After converting my projects to ASP.Net 2.0 I have seen a problem with
Page_unload. In 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


Mar 10 '06 #3

P: n/a
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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

"Karl Seguin [MVP]" wrote:
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...ver-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

--
http://www.openmymind.net/

"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
> Hi,
>
> After converting my projects to ASP.Net 2.0 I have seen a problem with
> Page_unload. In 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


Mar 10 '06 #4

P: n/a
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.

"Karl Seguin [MVP]" wrote:
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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

"Karl Seguin [MVP]" wrote:
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...ver-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

--
http://www.openmymind.net/

"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
> Hi,
>
> After converting my projects to ASP.Net 2.0 I have seen a problem with
> Page_unload. In 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


Mar 10 '06 #5

P: n/a
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cenk" <cenkoz2000@nspm_yahoo.com> wrote in message
news:1D**********************************@microsof t.com...
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.

"Karl Seguin [MVP]" wrote:
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
> 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
>
> "Karl Seguin [MVP]" wrote:
>
>> 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...ver-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
>>
>> --
>> http://www.openmymind.net/
>>
>>
>>
>> "Cenk" <Ce**@discussions.microsoft.com> wrote in message
>> news:82**********************************@microsof t.com...
>> > Hi,
>> >
>> > After converting my projects to ASP.Net 2.0 I have seen a problem
>> > with
>> > Page_unload. In 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
>>
>>
>>


Mar 10 '06 #6

P: n/a
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

"Cenk" wrote:
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.

"Karl Seguin [MVP]" wrote:
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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

"Karl Seguin [MVP]" wrote:

> 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...ver-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
>
> --
> http://www.openmymind.net/
>
>
>
> "Cenk" <Ce**@discussions.microsoft.com> wrote in message
> news:82**********************************@microsof t.com...
> > Hi,
> >
> > After converting my projects to ASP.Net 2.0 I have seen a problem with
> > Page_unload. In 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
>
>
>


Apr 26 '06 #7

P: n/a
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.

"Riccardo" wrote:
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

"Cenk" wrote:
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.

"Karl Seguin [MVP]" wrote:
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cenk" <Ce**@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
> 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
>
> "Karl Seguin [MVP]" wrote:
>
>> 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...ver-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
>>
>> --
>> http://www.openmymind.net/
>>
>>
>>
>> "Cenk" <Ce**@discussions.microsoft.com> wrote in message
>> news:82**********************************@microsof t.com...
>> > Hi,
>> >
>> > After converting my projects to ASP.Net 2.0 I have seen a problem with
>> > Page_unload. In 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
>>
>>
>>

Apr 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.