Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have page which has only gridveiw components. I wanted send a alert message to the user before he navigates to any other link. That is during the page unload below is my code. However the page navigates to the corresponding page but user is not prompted with alert message.

C#
protected override void OnUnload(EventArgs e)
{
    base.OnUnload(e);

    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Have you saved everything');window.open ('TestScriptPages.aspx');", true);

}
Posted

use window.onbeforeunload

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "window.onbeforeunload = function () {if(confirm('Have you saved everything')){window.open ('TestScriptPages.aspx');}}", true);
 
Share this answer
 
Hi instead of using alert use confirm it will ask your answer ok/cancel.

Just try this following code
JavaScript
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "if(confirm('Have you saved everything')){window.open ('TestScriptPages.aspx');}", true);
 
Share this answer
 
v2
Comments
ShaHam11 3-Jan-14 3:46am    
No luck its not working the alert message or confirm message is not prompted
ShaHam11 3-Jan-14 4:00am    
Actually if i just paste the above code under page load event the alert message is prompted I wanted this message to prompt, during page unload or when url changes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900