Solved

OnBeforeUnload .....

Posted on 2007-03-27
1
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

623 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