Solved

onbeforeunload with condition

Posted on 2006-07-23
3
554 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

12 Experts available now in Live!

Get 1:1 Help Now