Solved

OnBeforeUnload .....

Posted on 2007-03-27
1
309 Views
Last Modified: 2008-02-01
Hi,
I have an asp.net web page...and it has following script on the HTML <script> block
<script>
  var needToConfirm = <%= jsNeedToConfirm.ToString().ToLower() %>;
  window.onbeforeunload = confirmExit;
  function confirmExit()
    {
        if (needToConfirm )
        {
            return "Test";
        }
}
</script>
and in the code behind(im using C#) on the page_load event the variable
bool jsNeedToConfirm = true;
everything looks good till now....BUT...i have an asp.net button and in the click event of that button im setting jsNeedToConfirm = false;....now the problem im facing is that when im clicking the Save button....even before the click event is called the alert window is being popped up...and then its going to Click event of the button and setting the jsNeedToConfirm = false......can any one tell me what can be the problem or anyother suggestions.....
Prompt help on this wud be much appreciated...
Thanks
0
Comment
Question by:aspnetdev
1 Comment
 
LVL 1

Accepted Solution

by:
aspnetguy earned 500 total points
ID: 18800062
Without doing any testing it would seem that the code you wrote is working correctly (not necessarily how you want it to), let me explain.

When you click your form button it does a PostBack which means the page unloads and then reloads with the new data so the alert is appearing because JS is detecting the unload when you click your form button.

In your page load try setting this

btnName.Attributes.Add("onclick","needToConfirm=false;return true;");
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now