Solved

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

Posted on 2015-01-08
6
554 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:mwmiller78
6 Comments
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
Comment Utility
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 28

Expert Comment

by:Pravin Asar
Comment Utility
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 28

Expert Comment

by:Pravin Asar
Comment Utility
No Points please.

This is a better explaintion.

http://www.4guysfromrolla.com/webtech/100604-1.shtml
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mwmiller78
Comment Utility
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 74

Expert Comment

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

Author Comment

by:mwmiller78
Comment Utility
Yes, I use jquery elsewhere in my app
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

8 Experts available now in Live!

Get 1:1 Help Now