Little and Mickle

All things are difficult before they are easy.

导航

to call Session_End() event when user closed Browser

There is no event in the Global.asax to deal with browser closing. When we need to call Session_End() upon user close the browser. We can use a indirect way to call Session_End() to do some clearance jobs. Actually, we do not need to call Session_End() to do some clearance jobs upon closing browser in client.

Here's the steps:
1). to catch browser cloing event in .aspx page, simply, just add onbeforeunload event to the <body > tag.
 
<body onbeforeunload= 'CloseWindow();' >

</body>

2. In the head of .aspx page, add the following code to load another page to do clearance jobs. In the following codes, it would transfer to clearance.aspx page, and in the Page_Load() of clearance.aspx, we could do clearance job, or fire the Session_End() event by abandon Session.


<script language ="javascript">

function CloseWindow()
{
window.location = '../clearance.aspx';
alert('You are about to close the window');
}


</script>

 

3.in the Page_Load() of Clearance.aspx, add the following code to fireup Session_End() or do clearance(Attention, make sure your web application is in the In-Proc Session mode, web.config use In-Proc Session mode by default).

HttpContext.Current.Session.Abandon();

// to do clearance bellow:

// ...



Reference:
http://gridviewguy.com/ArticleDetails.aspx?articleID=108

posted on 2005-09-21 11:40  davidullua  阅读(1826)  评论(2)    收藏  举报