Link to home
Start Free TrialLog in
Avatar of aspnetdev
aspnetdev

asked on

Alert Before navigating to another page

hi,
Can any one tell me how do i alert a user before he moves away from a current page.Actually my asp.net page has a editable infragistic  datagird...now the problem im facing is users are editing the values and forgetting to press the save button and navigating to other pages....i would like to show a javascript pop up window if the user edit grid and tries to navigate to another page...is there any event on asp.net page which is called before going to another page?Can anyone help me on this.
Also,this question was posted previously but was not able to get any satisfactroy replies....there were suggestions saying that i call the alert on onUnloadEvent ...but what if user has to remaing on same page....
and once Unload is called,the page has to be unloaded and there's no way user can remain on same page.
Thanks
Avatar of dstanley9
dstanley9

you can return false from the onUnload event handler to cancel the navigation:

<body onUnload="return confirm("Are you sure you want to navigate away from this page?");">
Avatar of aspnetdev

ASKER

can you tell me how do i make a javascript variable which i set to true to false from code behind....
i have following javascript
var needToConfirm = true;
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm)
      return message to display in dialog box;
  }

on my code behind on click event of a button i have to make this as false...can you tell me how do i do it.
Thanks
add a property to the form class called _NeedToConfirm, then in your javascript add a replacement tag:

var needToConfirm = <%= _NeedToConfirm.ToString() %>;
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm)
      return message to display in dialog box;
  }

Then your code-behind will set the value of _NeedToConfirm, which will be inserted into the page when it is rendered.
can you be more elaborate because i aaded in my code behind
 bool jsNeedToConfirm = false;
and altered my function as follows

var needToConfirm = true;
var jsNeedToConfirm = <%= jsNeedToConfirm.ToString() %>;
window.onbeforeunload = confirmExit;
function confirmExit()
{
   if (needToConfirm && jsNeedToConfirm == null )
   return "Test";
}
actually i wanted that variable to be True by default except the Button Save is clicked...which is an asp.net button..so on the click event of the page i want to set this variable as false so that i dont get prompt on click save event which will do a post back
To set it to true by default use
 bool jsNeedToConfirm = true;

Then in the click event of your button set it to false.

Yout script will then be:

var jsNeedToConfirm = <%= jsNeedToConfirm.ToString() %>;
window.onbeforeunload = confirmExit;
function confirmExit()
{
   if (jsNeedToConfirm)
     return confirm("Are you sure you want to navigate away from this page?");
   else
     return true; // let the form close
}
actually it should be true by default but on click event it should me set to false...i mean if a user presses save button ihe shud not see the alert.
Also this script is being called on the OnUnloadEvent...so  i need to have a default value of the
var jsNeedToConfirm  in the <script> tagin the html code.
var jsNeedToConfirm = <%= jsNeedToConfirm.ToString() %>;

This will set it to whatever the value of jsNeedToConfirm is in your code behind.  If you initialize it to true and don't change it then the <%= %> tags will insert "true" into your script.
Stanley heres the clear scene of my problem....
actually i have a grid on my page...which is editable...users are actually making chages to the grid and forgettin to press save and navigating away from the page.so i added this script
var needToConfirm = true;
window.onbeforeunload = confirmExit;
function confirmExit()
{
   if (needToConfirm )
  return "Test";
}
now this works good....but again the problem im facing is,when save button is clicked...this prompt is being showed up...so my main concern is,is there anyway where i can make var needToConfirm = false
in the click event of my code....except for this click event it should be true for anything else.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Stanley im getting this error if im trying what u asked me to

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'jsNeedToConfirm' could not be found (are you missing a using directive or an assembly reference?)

Source Error:
Line 17:             <script>
Line 18:             var needToConfirm = <%= jsNeedToConfirm.ToString() %>;
Line 19:             
Line 20:             window.onbeforeunload = confirmExit;
 
do you have jsNeedToConfirm defined in your code-behind?

public bool jsNeedToConfirm = true;
Hi Stan,
one more werid error....it says
var needToConfirm = True; is undefined...
why wud it say so...cuz on page_load i made jsNeedToConfirm = true;any comments on this?
Thanks
var needToConfirm = True undefined; im getting this when debugging.
try changing to

var needToConfirm = <%= jsNeedToConfirm.ToString().ToLower() %>;
Stan,
your solution was excellent...but one more problem...if i click the save button even before the event is triggred the alert window pops up...if i press ok then its going to the event handler of the button....so its like even if i set the variable to false....the alert window pops up then click event is being called....any idea or suggestions how to over come this.
Thanks,