Solved

OnBeforeUnload .....

Posted on 2007-03-27
1
311 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

786 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