?
Solved

How do I warn the user of unsaved changes when leaving a page in ASP.NET(VB)?

Posted on 2015-01-08
6
Medium Priority
?
1,061 Views
Last Modified: 2015-04-11
I have seen a lot of JS examples, and I'm not sure any of them work for me. I have a lot of controls inside repeaters and datagrids so it makes it cumbersome to track in a client script. Is there a simple way to check if any data in any control has changed since the page load and then generate a warning message?
0
Comment
Question by:Mike Miller
[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
6 Comments
 
LVL 82

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 40539581
var unsaved = false;
$(":input").change(function(){ //trigers change in all input fields including text type
    unsaved = true;
});

function unloadPage(){ 
    if(unsaved){
        return "You have unsaved changes on this page. Do you want to leave this page and discard your changes or stay on this page?";
    }
}

window.onbeforeunload = unloadPage;

Open in new window

http://bit.ly/1KrqqSA
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 40540682
Try the onbeforeunload event: It is fired just before the page is unloaded. It also allows you to ask back if the user really wants to leave. See the demo onbeforeunload Demo.

Alternatively, you can send out an Ajax request when he leaves.

For detailed/field level tracking, you may look at example code @

http://www.telerik.com/forums/warn-users-before-leaving-a-page-with-uncommitted-changes
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 40540698
No Points please.

This is a better explaintion.

http://www.4guysfromrolla.com/webtech/100604-1.shtml
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Mike Miller
ID: 40541956
David, what am i missing here?

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="test.aspx.vb" Inherits="testproj.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office" lang="en-us" dir="ltr">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
     <script type="text/javascript">
            var unsaved = false;
            $(":input").change(function () { //trigers change in all input fields including text type
                unsaved = true;
            });

            function unloadPage() {
                if (unsaved) {
                    return "You have unsaved changes on this page. Do you want to leave this page and discard your changes or stay on this page?";
                }
            }

            window.onbeforeunload = unloadPage;
    </script>
</body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40553283
Have you imported the JQuery library?
0
 

Author Comment

by:Mike Miller
ID: 40574301
Yes, I use jquery elsewhere in my app
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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