Handle Browser close event by pressing [X]
-
button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003
Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy
-
button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003
Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy
koolprasad2003 wrote:
t but for that i need to repeate same code on each page
If you are using master pages in your application, you wont have to write same code on each page. On other thought, you can create a base page and write code to inject javascript function using ScriptManager. And then make all pages to inherit the base class.
-
button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003
Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy
There is no any permanent connection between your browser and the server. So you need to detect browser close event from client side (browser) and then call server event to execute your code. You can use master page to avoid writing repeating code on each page. Find following code for the same. 1. To enable page methods
<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />
2. Detect browser close event.
<body onunload="HandleClose()">
3. Call pagemethods to invoke server side event.
<script language="javascript" type="text/javascript">
//<![CDATA[
function HandleClose()
{
alert("Killing the session on the server!!");
PageMethods.AbandonSession();
}
//]]>
</script>4. Define your page methods at server side and call your code.
[WebMethod]
public static void AbandonSession()
{
HttpContext.Current.Session.Abandon();
}HTH
Jinal Desai - LIVE Experience is mother of sage....
-
button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003
Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy
Have a look to trick http://www.codeproject.com/Tips/41771/How-to-detect-browser-close-when-browser-X-is-clic.aspx Hope it will help you
Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0
-
There is no any permanent connection between your browser and the server. So you need to detect browser close event from client side (browser) and then call server event to execute your code. You can use master page to avoid writing repeating code on each page. Find following code for the same. 1. To enable page methods
<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />
2. Detect browser close event.
<body onunload="HandleClose()">
3. Call pagemethods to invoke server side event.
<script language="javascript" type="text/javascript">
//<![CDATA[
function HandleClose()
{
alert("Killing the session on the server!!");
PageMethods.AbandonSession();
}
//]]>
</script>4. Define your page methods at server side and call your code.
[WebMethod]
public static void AbandonSession()
{
HttpContext.Current.Session.Abandon();
}HTH
Jinal Desai - LIVE Experience is mother of sage....
If you have a button or an imagebutton in the masterpage you can place the next code in you HandleClose() function:
__doPostBack('<%= Button.UniqueID %>','');
or__doPostBack('<%= ImageButton.UniqueID %>','');
In that case you come in the OnClick event of the button or the imagebutton and there you can handle the Abandon of your SessionIn Word you can only store 2 bytes. That is why I use Writer.