Solved

OnBeforeUnload .....

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now