zohair
zohair

Reputation: 2369

doing an action when the page exits

I have an ASP.NET(2.0 C#) Web application, and I wanted to know how to do an action once the user moves to another page or leaves the application.

Is there a method like page_load is for when the user enters the page, except this one is for exiting the page?

Thanks

Upvotes: 1

Views: 345

Answers (2)

kervin
kervin

Reputation: 11858

You can handle the Javascript 'onunload' or 'onbeforeunload' events.

See the articles "Using ASP.NET to Prompt a User to Save When Leaving a Page " and "Prompting a User to Save When Leaving a Page " for more information and ASP.Net specific examples.

These Javascript events are also described in the MSDN library...

onunload Event

onbeforeunload Event

Upvotes: 1

Tom Ritter
Tom Ritter

Reputation: 101400

You would need to use javascript to tell the server when the user leaves the page. The webserver washes it's hands of the page once it leaves the server, while the user might keep the page open for a week.

If you use javascript on the page, when the page unloads to fire off a notice to your server you can take some action.

Although, you can't tell if he's leaving your page for another one of your pages, another website, or closing the browser. And that last notice you fired isn't guaranteed to always be sent, so you can't rely on it completely.

What's the scenario you're looking for? There might be a better way to accomplish what you're trying to do.

Upvotes: 3

Related Questions