Warn user before leaving page with unsaved changes

Discussion in 'ASP .Net' started by markalroberts@gmail.com, Oct 25, 2005.

  1. Guest

    Hi,

    I wish to ask warn the user that there are unsaved changes (if there
    are) and allow them to cancel navigating away/closing the browser.

    Investigation leads me to believe the "OnBeforeUnload" event is the
    best bet - this does as I wish... Except it doesn't distinguish between
    hyperlinks within the site and hyperlinks outside it.

    I.e. simple Hyperlink button will raise the Unload event when clicked,
    as will closing the window, but I'm only interested in the Closing
    window one, and there doesn't appear to be any way to tell them apart.

    If someone could help here, it would be very useful!
    Thanks,
    Mark.
    , Oct 25, 2005
    #1
    1. Advertising

  2. Guest

    , Oct 25, 2005
    #2
    1. Advertising

  3. sp3d2orbit Guest

    I've done something similar to that. Ignoring the hyperlinks and only
    responding to a close involves some manual coding. Start with this
    block of javascript.

    var IsLinkClicked = false;

    function CheckUnload()
    {
    if (!IsLinkClicked && ("1" ==
    document.forms["Form1"].IsChanged.Value))
    {
    if (confirm("You have unsaved changes. Click OK to save changes."))
    {
    document.forms["Form1"].submit();
    alert("Changes saved.");

    return false;
    }
    }
    }

    First thing, you'll want to set a hidden fields 'IsChanged' to true
    whenever you make a change that should be saved. This way you'll only
    prompt when a change has actually been made. Secondly, if the user
    clicks on any hyperlinks on your page you need to set 'IsLinkClicked' =
    true using javascript whenever a user clicks on a link. That way you'll
    only prompt when the user closes the window or manually types in a new
    address into the URL bar.

    Instead of a hidden field for IsChanged, you could probably implement
    this as a javascript variable also.
    sp3d2orbit, Oct 25, 2005
    #3
  4. When it is a planned navigation (like hiperlink , button etc) set a
    javascript variable and check this variable in OnBeforeUnload to determine
    whether to display warning message or not ..

    for eg, declare javscript variable

    var warn_pageunload = true;

    Fron button click , link click etc set

    warn_pageunload =false;

    In the unload event

    if(warn_pageunload == true)
    {
    \\code to warn user
    }

    "" wrote:

    > Hi,
    >
    > I wish to ask warn the user that there are unsaved changes (if there
    > are) and allow them to cancel navigating away/closing the browser.
    >
    > Investigation leads me to believe the "OnBeforeUnload" event is the
    > best bet - this does as I wish... Except it doesn't distinguish between
    > hyperlinks within the site and hyperlinks outside it.
    >
    > I.e. simple Hyperlink button will raise the Unload event when clicked,
    > as will closing the window, but I'm only interested in the Closing
    > window one, and there doesn't appear to be any way to tell them apart.
    >
    > If someone could help here, it would be very useful!
    > Thanks,
    > Mark.
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 25, 2005
    #4
  5. incase you have not come across this

    http://msdn.microsoft.com/library/d...hor/dhtml/reference/events/onbeforeunload.asp

    "" wrote:

    > Hi,
    >
    > I wish to ask warn the user that there are unsaved changes (if there
    > are) and allow them to cancel navigating away/closing the browser.
    >
    > Investigation leads me to believe the "OnBeforeUnload" event is the
    > best bet - this does as I wish... Except it doesn't distinguish between
    > hyperlinks within the site and hyperlinks outside it.
    >
    > I.e. simple Hyperlink button will raise the Unload event when clicked,
    > as will closing the window, but I'm only interested in the Closing
    > window one, and there doesn't appear to be any way to tell them apart.
    >
    > If someone could help here, it would be very useful!
    > Thanks,
    > Mark.
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 25, 2005
    #5
  6. elizas

    Joined:
    Jan 27, 2010
    Messages:
    27
    Location:
    India
    Warning the user before leaving the page !!!

    You might have seen in many of the web form pages to warn the user before closing the page.When somebody refreshes the page, then there is a chance for loosing all filled data. In that case it is very helpful.
    Following is the code :



    <script type="text/javascript">
    var shouldsubmit = false; // Represent the button should ask for confirmation or not.
    window.onbeforeunload = confirmUnloading;



    // Funcion called before unloading of the page.
    function confirmUnloading()
    {
    if (!shouldsubmit)
    {
    return "If you have any unsaved data in the current page, it will be lost.";
    }
    }
    </script>


    <asp:Button ID="btnSubmit" runat="server"
    Text="Submit"
    CssClass = "btnPrimary"
    OnClientClick="javascript:shouldsubmit=true;"
    ToolTip="Submit Page"/>

    Thanks
    elizas, Jan 27, 2010
    #6
  7. elizas

    Joined:
    Jan 27, 2010
    Messages:
    27
    Location:
    India
    Warning the user before leaving the page

    ou might have seen in many of the web form pages to warn the user before closing the page.When somebody refreshes the page, then there is a chance for loosing all filled data. In that case it is very helpful.


    Page life cycle includes two events like onunload and onbeforeunload. For this case you need to bind the script function in window. Onbeforeunload so that it will be called when page is unloading.

    Again this warning should not be fired when you are actually submitting the page. For that set a boolean value (e.g. shouldsubmit) to submit the page.

    Following is the code :



    <script type="text/javascript">
    var shouldsubmit = false; // Represent the button should ask for confirmation or not.
    window.onbeforeunload = confirmUnloading;
    elizas, Apr 27, 2010
    #7
  8. rohit.net100

    Joined:
    Feb 28, 2011
    Messages:
    1
    Hi,

    How to add this code for checkbox and dropdownlist instead of button.


    rohit.net100, Feb 28, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    1
    Views:
    1,526
    Tomer Ben-David
    Oct 27, 2004
  2. winston

    Setting unsaved in MSWord

    winston, Aug 22, 2007, in forum: XML
    Replies:
    1
    Views:
    370
    Joseph Kesselman
    Aug 22, 2007
  3. JT
    Replies:
    2
    Views:
    88
    Aaron [SQL Server MVP]
    Jun 28, 2004
  4. Jamie Jackson

    Warn to Save before leaving a page...

    Jamie Jackson, Oct 9, 2003, in forum: Javascript
    Replies:
    5
    Views:
    150
    Jamie Jackson
    Oct 10, 2003
  5. koruyucu
    Replies:
    2
    Views:
    156
    Thiago Macedo
    Jul 30, 2008
Loading...

Share This Page