• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Need to run the update script when the user exits the page - ASP.NET (C#)


Need to do two things:

1. Run one update statement when user comes on a particular page
2. Run another update when user exists that page.

Sr.no 1 can be done on Page_Load event.

For Srno.2,  User can exit the page in two ways:
(a) Closing the browser
(b) Clicking the link of other page.

How do I run the update when the user exists the page through above two cases (i.e. a & b)?


2 Solutions
Rahul Goel ITILSenior Consultant - DeloitteCommented:
See, both approaches can be achieved using javascript event handlers. and make a ajax call to the server
Here is the code for trapping windows close

<title>Detecting browser close in IE</title>
<script type="text/javascript">
var myclose = false;
function ConfirmClose()
if (event.clientY < 0)
event.returnValue = 'Any message you want';
function HandleOnClose()
if (myclose==true) alert("Window is closed");
<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">
<h4>Close browser!</h4>

Open in new window

meetpdAuthor Commented:
Can I put an sql update statement in this? How?

Sorry for the silly question, but I am a newbie.
U can call any evnet from the javascript function:

Put a Attribute ScriptMethod: on the Method to be called
 and call this from javascript call


in ur cs file


  //Update Query
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now