Prevent user using Browser back button RRS feed

  • Question

  • User-809753952 posted

    I have a webpage with a detailed view. The user can edit and save the detailed view data.  The user is able to click on the browser back button after entering some data in edit mode and leave page without saving the entered data. How can I prevent it?

    Monday, July 30, 2018 1:09 PM

All replies

  • Monday, July 30, 2018 1:23 PM
  • User-809753952 posted

    Dear Mudassarkhan! Thanks for your reply. I don't want to diable the back button. I just want to warn the user to save the data.

    Monday, July 30, 2018 1:50 PM
  • User-1171043462 posted


        $(window).on('beforeunload', function () {
            if ($('.changed-input').length) {
                return 'You haven\'t saved your changes.';

    Ref: https://www.bitnative.com/2013/08/19/warn-users-of-unsaved-changes-with-jquery/

    Monday, July 30, 2018 1:58 PM
  • User36583972 posted

    Hi mnmhemaj,

    You can refer the following code. The alert message will appear on any click event like back button, hyperlinks, etc execute on every postback.

     <script type="text/javascript">
            var isFiredTwice = false;
            window.onbeforeunload = confirmUnloading;
            // Funcion called before unloading of the page.
            function confirmUnloading() {
                if (navigator.appName == "Microsoft Internet Explorer") // For IE
                    if (!isFiredTwice) {
                        event.returnValue = "If you have any unsaved data in the current page, it will be lost."
                        isFiredTwice = true;
                        setTimeout("isFiredTwice = false;", 0)
                else {
                    // For FF
                    return "If you have any unsaved data in the current page, it will be lost.";

    The following thread's solution provided by the Rion Williams for your reference.

    How to warn user while leaving the page using asp.net c#

    Best Regards,

    Yong Lu

    Tuesday, July 31, 2018 2:42 AM
  • User-1034726716 posted

    You can't. This is entirely a browser control and your code can't access them.

    However, you could probably force the cache to expire to disable the browser back button or you could try using this JavaScript hack as what muddasar is suggusted.

    Tuesday, July 31, 2018 3:06 PM
  • User-809753952 posted

    My page has more than one detailview and many submit buttons. Muddasar's script is triggered, whenever a post back occurs in the page. Is it possible to prevent user clicking browser back button, only when a detailview form data is not saved? 

    Wednesday, August 1, 2018 7:26 AM
  • User-1171043462 posted

    NO. Because on PostBack also page is refreshed.

    Thus you need to switch to AJAX for it then so that PostBack does not happen.

    Use UpdatePanel

    Wednesday, August 1, 2018 8:51 AM
  • User753101303 posted


    As pointed already, it seems you rather want to warn the user when the page is about to be unloaded and changes were done rather than really to disable the back button...

    Wednesday, August 1, 2018 8:55 AM