Hi-
Thanks a lot for your help. I did know the client side code. I wish complement reply with the server side code.
My original question is "How to know if user close the browser or navigate to another page ?"... missing: "I need run something in backend".
Right, to send an event to server side from client, we need a callback. I am working an approach that’s works fine:
Simple sample:
...
using System.IO;
...
public partial class test : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
string er = ClientScript.GetCallbackEventReference(this, "", "null", "null", "null", true);
string rb = "function callServerTask(arg) {" + er + ";}";
cs.RegisterClientScriptBlock(this.GetType(), "callServerTask", rb, true);
// body has attribute rutat=server id=body
body.Attributes.Add("onunload", "callServerTask('');return false;");
}
public void RaiseCallbackEvent(string eventArgument)
{
CloseMe();
}
public string GetCallbackResult()
{
return "";
}
private void CloseMe()
{
// to test that callback works
StreamWriter s = new StreamWriter(Server.MapPath("closehistory.txt"), true);
s.WriteLine("close at:" + DateTime.Now.ToString());
s.Close();
}
}
Warning: In IDE of Visual Studio works when navigate to another page. In production, works always.
Regards.