Solved

onbeforeunload with condition

Posted on 2006-07-23
3
558 Views
Last Modified: 2008-02-01
In ASP.NET 2.0 with C#
I want to use the command onbeforeunload in order to check if the user presses the close button:
<body onbeforeunload="return ' '">
but this command shows the message each time the page is refreshed.
Can I give a condition that it will not appear in each refresh?
for example the following code check if the value in the page was changed and only if true the onbeforeunload
will active.
I need to do it in .NET and check if the values in the formview in the page were changed and not in each
refresh of the page.
How do I do it?

<head>
<script>
var oldVal = "";
function saveValues()
{   oldVal = document.myForm.tester.value;}

function checkChanges()
{  if ( oldVal != document.myForm.tester.value )
   { return 'Are you sure you wish to exit dude?';}}

</script>
</head>
<BODY onload="saveValues();" onbeforeunload="return checkChanges();">
<form name="myForm">
<input type="text" name="tester">
</form>
</body>
0
Comment
Question by:ANAT2403
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17162961
Here is an option:

-----------------------------------
On Any Forms Want to detect
-----------------------------------
detect close ie X button ( close window)
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4878&lngWId=10

<HEAD>
<SCRIPT language="JavaScript" >
function detactCloseWin() {
    var top=self.screenTop;
     if (top>9000) {
        window.open("closeWindow.aspx","_blank","fullscreen=0,width=0,height=0,
                             scrollbars=0,toolbar=0,menubar=0,status=0");
    }
}
</SCRIPT>
</HEAD>
<BODY onunload="detactCloseWin();" MS_POSITIONING="GridLayout">

-------------------
On new window
-------------------
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
     General.chat.Delete_ActiveUser()
     Response.Write("<script language='javascript'> { window.close();}</script>")
End Sub

Bob
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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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