Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

QuestionHow to implement this when I am using master page Pinmembersrmohanr27-May-14 1:52 
GeneralMay be Dangerous PinmemberTheyCallMeMrJames25-Jun-10 7:41 
GeneralRe: May be Dangerous PinmemberAnuj Khadloya27-Jun-10 19:36 
GeneralThis will only work in IE Pinmembersob25-Jun-10 3:46 

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

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

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