to call a function in the Unload phase of the asp.net server side page lifecyle you would do something like:
protected override void OnUnload(EventArgs e){
MyMethod();//here your calling a method called "MyMethod"
}
If your attempting to call a server side method as part of the client side dom unload event I would seriously encourage you to abandon this endeavour. You can not reliably capture this event. If i hit Alt+F4 in the browser the browser will close without
sending the event to the server, thus you can't depend on the event triggering some server side code.
If you just want to call a client side javascript method on client side dom unload event (no server side activity) then you can just follow Manikandan's example.
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
I hear what you're saying but you CAN NOT reliably/consistently capture a page closing event from the server side. This is part of the problem with http being a stateless protocol. Don't try to fight it by writing some javascript monster that only works
some of the time.
Find a different solution. Now you say you have a delete method on the server that you want to run when the user leave the page. What are u deleting? A file? A record in a database? Whatever it is, I will refer to it as the "resource".
I don't usually advocate using the Session class but i will this time. I suggest storing the resource as a session variable. This way as part of the session expiry event you can delete the resource. Also if the user leaves the page then comes back you can
do a check like:
if (!Page.IsPostBack && Page.Session["UserResource"] != null)
DeleteMyResource(Page.Session["UserResource"]);
The Session_End event should be in your global.asax file.
If the resource is a file just store the path to the file (not the contents of the file)
Something like this would be a much better way of managing a user-session relevant resource on the server.
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
As Sam Critchley told we should not depend on the client side unload event, because that’s not in our control when the user did some kind of unusual attempts like alt+f4.
If I am not wrong you can use Session_End method which is located in global.asax, there you can add logic to perform some operation when the users exit from your application.
sesionglobal.asax
[Manikandan Balakrishnan]
Dont forget to click "Mark as Answer" on the post that helped you.
hi i was written a method to delete a record in a table, i want to call this function while i am closing a WebPage. please help me, i want detail information for this. please help me,
hi i was written a method to delete a record in a table, i want to call this function while i am closing a WebPage. please help me, i want detail information for this. please help me,
global.asax.cs containt a method called " Session_End() "
try to call your logout code trough this method.
-----
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
rprashanthku...
Member
2 Points
87 Posts
page unload
Jun 20, 2007 06:07 AM|LINK
hi
please help me in while closing the page i want to call a function in Page Unload Event,
please help me
urgent.
regards,
Prashanth
Manikandan.n...
Member
169 Points
28 Posts
Re: page unload
Jun 20, 2007 06:20 AM|LINK
Hi,
You are not mentioned that you want to call a server side method, or a JavaScript method. Here I given example for javascript method.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="unlaod.aspx.cs" Inherits="unlaod" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script language="javascript"> function unload() { alert("You are moving to Microsoft Live.... :)"); } window.onunload=unload; </script> </head> <body> <form id="form1" runat="server"> <div> <a href="http://www.live.com">MicroSoft Live</a> </div> </form> </body> </html>To call a server side Method:
You can use the Ajax to invoke server side method, from the above javascript function
javascript c# ajax Atlas asp .net ... c# ... SQL
Dont forget to click "Mark as Answer" on the post that helped you.
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: page unload
Jun 20, 2007 06:31 AM|LINK
to call a function in the Unload phase of the asp.net server side page lifecyle you would do something like:
protected override void OnUnload(EventArgs e){
MyMethod();//here your calling a method called "MyMethod"
}
If your attempting to call a server side method as part of the client side dom unload event I would seriously encourage you to abandon this endeavour. You can not reliably capture this event. If i hit Alt+F4 in the browser the browser will close without sending the event to the server, thus you can't depend on the event triggering some server side code.
If you just want to call a client side javascript method on client side dom unload event (no server side activity) then you can just follow Manikandan's example.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
rprashanthku...
Member
2 Points
87 Posts
Re: page unload
Jun 20, 2007 06:35 AM|LINK
hi
I want to call the server side function, i have a delete method in C# code in ASP.NET , i want to call that function when i will close the page
please help me, urgent
thanks & Regards
Prashanth
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: page unload
Jun 20, 2007 07:01 AM|LINK
Hi Prashanth,
I hear what you're saying but you CAN NOT reliably/consistently capture a page closing event from the server side. This is part of the problem with http being a stateless protocol. Don't try to fight it by writing some javascript monster that only works some of the time.
Find a different solution. Now you say you have a delete method on the server that you want to run when the user leave the page. What are u deleting? A file? A record in a database? Whatever it is, I will refer to it as the "resource".
I don't usually advocate using the Session class but i will this time. I suggest storing the resource as a session variable. This way as part of the session expiry event you can delete the resource. Also if the user leaves the page then comes back you can do a check like:
if (!Page.IsPostBack && Page.Session["UserResource"] != null)
DeleteMyResource(Page.Session["UserResource"]);
The Session_End event should be in your global.asax file.
If the resource is a file just store the path to the file (not the contents of the file)
Something like this would be a much better way of managing a user-session relevant resource on the server.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
Manikandan.n...
Member
169 Points
28 Posts
Re: page unload
Jun 20, 2007 07:04 AM|LINK
As Sam Critchley told we should not depend on the client side unload event, because that’s not in our control when the user did some kind of unusual attempts like alt+f4.
If I am not wrong you can use Session_End method which is located in global.asax, there you can add logic to perform some operation when the users exit from your application.sesion global.asax
Dont forget to click "Mark as Answer" on the post that helped you.
rprashanthku...
Member
2 Points
87 Posts
Re: page unload
Jun 20, 2007 07:18 AM|LINK
hi i was written a method to delete a record in a table, i want to call this function while i am closing a WebPage. please help me, i want detail information for this. please help me,
urgent,
Thanks & Regards
Prashanth
rprashanthku...
Member
2 Points
87 Posts
Re: page unload
Jun 20, 2007 07:20 AM|LINK
hi i was written a method to delete a record in a table, i want to call this function while i am closing a WebPage. please help me, i want detail information for this. please help me,
urgent,
Thanks & Regards
Prashanth
Manikandan.n...
Member
169 Points
28 Posts
Re: page unload
Jun 20, 2007 07:28 AM|LINK
Can you post the delete method what you wrote, is it related to current user?
Dont forget to click "Mark as Answer" on the post that helped you.
bachbouch
Member
482 Points
86 Posts
Re: page unload
Jun 20, 2007 08:00 AM|LINK
global.asax.cs containt a method called " Session_End() "
try to call your logout code trough this method.
-----
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.