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

Execute server side code on close of a browser

By , 26 Jun 2010
Rate this:
Please Sign up or sign in to vote.
To start with create a new asp.net 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.
 
[WebMethod]
public static void BrowserCloseMethod()
{
    // Write Custom Code
    HttpContext.Current.Session.Abandon();
}   

 
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() 
{
	PageMethods.BrowserCloseMethod();
}
</script>
 
<body onunload="Browser_Close()"></body>

License

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

 
GeneralMay be Dangerous PinmemberTheyCallMeMrJames25-Jun-10 6:41 
GeneralRe: May be Dangerous PinmemberAnuj Khadloya27-Jun-10 18:36 
GeneralThis will only work in IE Pinmembersob25-Jun-10 2: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 | Mobile
Web04 | 2.8.140415.2 | Last Updated 26 Jun 2010
Article Copyright 2010 by Anuj Khadloya
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid