Click here to Skip to main content
12,896,568 members (58,056 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 24 Jun 2010

Execute server side code on close of a browser

, 26 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article provides a solution to the age-long question of "How do I execute some server side code when my browser is closed?". The answer to this question is AJAX.
To start with create a new page and add an instance of the ScriptManager to it. Now configure the ScriptManager to enable calling of page methods marked with WebMethod attribute.

<asp:scriptmanager id="Scriptmanager1" runat="server" enablepagemethods="true" xmlns:asp="#unknown"></asp:scriptmanager>

The next step is to create a web method on the server. The method needs to be marked with the WebMethod attributte.

public static void BrowserCloseMethod()
    // Write Custom Code

Finally call the server method on closing the client browser window. Add a javascript method and call this method when the unload event for body of the browser is called.

<script language="javascript" type="text/javascript">
function Browser_Close() 
<body onunload="Browser_Close()"></body>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anuj Khadloya
Technical Lead Kale Consultants Ltd.
India India
No Biography provided

You may also be interested in...

Comments and Discussions

PraiseIt worked well Pin
poonam39316-Mar-17 6:17
memberpoonam39316-Mar-17 6:17 
QuestionHow to implement this when I am using master page Pin
srmohanr27-May-14 0:52
membersrmohanr27-May-14 0:52 
GeneralMay be Dangerous Pin
TheyCallMeMrJames25-Jun-10 6:41
memberTheyCallMeMrJames25-Jun-10 6:41 
GeneralRe: May be Dangerous Pin
Anuj Khadloya27-Jun-10 18:36
memberAnuj Khadloya27-Jun-10 18:36 
GeneralThis will only work in IE Pin
sob25-Jun-10 2:46
membersob25-Jun-10 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Jun 2010
Article Copyright 2010 by Anuj Khadloya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid