开发者

C# ASP.NET Page Leaving event?

I was looking for an event that would fi开发者_StackOverflow社区re or a way to tell if the user was leaving a page. Either to navigate to another page or closing the page alltogether. Is this possible through the events that fire?


Not in ASP.NET per se.

You will have to write some front-end javascript to do this using something like window.onbeforeunload(). Then you'd have to make an AJAX call to tell your back-end that this event is happening. This isn't foolproof, of course. A browser crash or a forced "quit" would not fire this event.


Not through server-side events. Detecting page-leaving requires JavaScript.


Client-side, you can use onunload() (or onbeforeunload() to give an "are you sure" that they will be grateful for if it meant they stopped accidentally closing something after writing a long stackoverlow.com post, but will annoy users in most cases).

Server-side, you can't really, though you could have the onunload do something to signal to the server.

As a rule though, this indicates you are treating the web as stateful, rather than stateless. Stateless designs work better with stateless protocols like the web's; the more your application treats each request as unique and independent of previous requests (though of course, just what that request is will depend on how the client dealt with the previous request) the more scalable and reliable your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜