Well probably Application_End will not be the right direction as you don't know when it will be called. It doesn't mean that when you close your browser application end event will call. However you can try using Session_End event and it will be called when
the session will expire. So even if you close the browser Session_End event will be called when it reaches the timeout period which is by default 20 minutes.
Faraz Shah Khan
SFDC Certified Developer, MCP, MCAD.Net, MCSD.Net, MCTS-Win/Web, MCPD-Web
Blog
return valueSent;
}
function doUpdate()
{
var webUrl = 'AjaxPage.aspx';
// Add any additional values needed for the update to the QueryString...
var queryString = '?CallRequest=UpdateDatabase&DbKey=1234';
var returnCode = callAjax(webUrl, queryString);
//alert('returnCode: ' + returnCode);
}
function windowOnBeforeUnload()
{
if ( g_isPostBack == true )
return; // Let the page unload
if ( window.event )
window.event.returnValue = 'You will lose any unsaved changes!'; // IE
else
return 'You will lose any unsaved changes!'; // FX
}
function windowOnUnload()
{
if ( g_isPostBack == true )
return; // Let the page unload
sweetyPaul
Member
5 Points
36 Posts
application end
Apr 27, 2009 10:51 AM|LINK
Hi all,
how to save the data in an application when that application(browser)closing.
rgds
sp
kavita_khand...
Star
9767 Points
1930 Posts
Re: application end
Apr 27, 2009 11:30 AM|LINK
if user is closing the bwoser using the cross button at the top right.. i think u can not do anything about it.
I would love to change the world, but they wont give me the source code.
farazsk11
Star
8347 Points
1384 Posts
Re: application end
Apr 27, 2009 11:54 AM|LINK
Hi,
Well probably Application_End will not be the right direction as you don't know when it will be called. It doesn't mean that when you close your browser application end event will call. However you can try using Session_End event and it will be called when the session will expire. So even if you close the browser Session_End event will be called when it reaches the timeout period which is by default 20 minutes.
SFDC Certified Developer, MCP, MCAD.Net, MCSD.Net, MCTS-Win/Web, MCPD-Web
Blog
avicool08
Member
710 Points
272 Posts
Re: application end
Apr 27, 2009 11:58 AM|LINK
Try this:
<script type="text/javascript">
<!--
var g_isPostBack = false;
function onBeforeUnload()
{
if ( g_isPostBack == true )
return; // Let the page unload
if ( window.event )
window.event.returnValue = 'You will lose any unsaved changes!'; // IE
else
return 'You will lose any unsaved changes!'; // FX
}
window.onbeforeunload = onBeforeUnload;
// -->
</script>
In the CodeBehind:
private void Page_Load(object sender, System.EventArgs e)
{
RegisterOnSubmitStatement("OnSubmitScript", "g_isPostBack = true;");
}
http://forums.asp.net/t/1370273.aspx
Abinash Tumulu
Please mark my reply as an
answer
if it helps.
Visit my blog .net Scanner
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: application end
Apr 27, 2009 12:45 PM|LINK
To save data to a database when a page is closed, you will need to use AJAX. Here is an example:
//////////////////////////////////////////////////////////////////////////
// Page1.aspx
//////////////////////////////////////////////////////////////////////////
<script type="text/javascript">
<!--
var g_isPostBack = false;
function callAjax(webUrl, queryString)
{
var xmlHttpObject = null;
try
{
// Firefox, Opera 8.0+, Safari...
xmlHttpObject = new XMLHttpRequest();
}
catch(ex)
{
// Internet Explorer...
try
{
xmlHttpObject = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(ex)
{
xmlHttpObject = new ActiveXObject('Microsoft.XMLHTTP');
}
}
if ( xmlHttpObject == null )
{
window.alert('AJAX is not available in this browser');
return;
}
xmlHttpObject.open("GET", webUrl + queryString, false);
xmlHttpObject.send();
var valueSent = xmlHttpObject.responseText;
return valueSent;
}
function doUpdate()
{
var webUrl = 'AjaxPage.aspx';
// Add any additional values needed for the update to the QueryString...
var queryString = '?CallRequest=UpdateDatabase&DbKey=1234';
var returnCode = callAjax(webUrl, queryString);
//alert('returnCode: ' + returnCode);
}
function windowOnBeforeUnload()
{
if ( g_isPostBack == true )
return; // Let the page unload
if ( window.event )
window.event.returnValue = 'You will lose any unsaved changes!'; // IE
else
return 'You will lose any unsaved changes!'; // FX
}
function windowOnUnload()
{
if ( g_isPostBack == true )
return; // Let the page unload
doUpdate();
}
window.onbeforeunload = windowOnBeforeUnload;
window.onunload = windowOnUnload;
// -->
</script>
//////////////////////////////////////////////////////////////////////////
// Page1.aspx.cs
//////////////////////////////////////////////////////////////////////////
protected void Page_Load(object sender, EventArgs e)
{
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), "OnSubmitScript", "g_isPostBack = true;");
}
//////////////////////////////////////////////////////////////////////////
// AjaxPage.aspx.cs
//////////////////////////////////////////////////////////////////////////
protected string UpdateDatabase(string dbKey)
{
string returnValue = "OK";
// Update the database here setting the returnValue variable to "Failed" if not successful...
return returnValue;
}
private void Page_Load(object sender, System.EventArgs e)
{
string callRequest = (this.Request["CallRequest"] == null) ? string.Empty : this.Request["CallRequest"];
string returnValue = string.Empty;
if ( callRequest == "UpdateDatabase" )
{
string dbKey = (this.Request["DbKey"] == null) ? string.Empty : this.Request["DbKey"];
returnValue = UpdateDatabase(dbKey);
}
this.Response.ClearHeaders();
this.Response.Clear();
this.Response.Write(returnValue);
this.Response.End();
}
NC...