Machine translation:
Creating software for businesses, professionals, and the home
our sites: MBBSoftware - Information Presenter - Authenticate Testimonial
MBBSoftware Blog - How to Prevent Unwanted Browser Refresh Hi guest
Sign up

How to Prevent Unwanted Browser Refresh

By Miroslav B. Bonchev
Web developers are sometimes faced with a slightly annoying problem related to preventing the server from acting when a browser refreshes. The F5 or browser refresh is sometimes unpleasant since the browser sends to the server its last request, repeating it as it was, and ignoring the current state of the page. Thus if the last request, for example, made an entry in a database on the server, then each page refresh would result in a new attempt to add a new entry to the database, and if no measures are taken a new unwanted entry will be added to the database. To the best of my knowledge, it is not possible to prevent refresh through JavaScript or otherwise on the client side, so it has to be done on the server. In this article I will give one possible solution which is easy to implement and safe to use.

The solution proposed in this article is to use a session variable, which would keep record of the state and be used by the code behind to make the appropriate decisions. The session variable will have three recognized states "Enabled", "Disabled" and NULL. The reader probably has already imagined what is needed in order to overcome the problem and make their own particular implementation, but nevertheless I will give a pseudo code example which is derived from production code which I have written.

Suppose that we have a form with several controls used by the website visitor to enter data which will be recorded in a database. When they click the "Submit" button, the form is sent to the server which adds the submitted data to the database. I will assume that the page has a "Confirmation" checkbox which is used to send the form to the server for: (a) consistency verification on the server, and (b) visitor verification of its truthfulness before the "Submit" button is clicked. Typically the "Confirmation" checkbox disables the user data enter controls on-check and enables them on uncheck. Thus when the data is "confirmed" the user cannot modify the contents of the controls but can only visually confirm it, and either click the "Submit" button or uncheck the "Confirmation" checkbox. To resolve the Browser Refresh problem we will use a session variable in the confirmation checkbox on-check event on the server side.

              [Server Side]
              void On_Confirmation_Checkbox_Check_Enent()
                if( "Confirmation" checkbox is checked )
                  1. Confirm that all entered data is consistent and acceptable 
                     – if not uncheck the checkbox and return error message

                  2. Disable all user data enter controls

                  // Establish a session variable that the session is valid and allow data to be added to the database
                  Session["EnableDatabaseProcessing"] = "Enable";
                  1. Enable all user data enter controls

                  // Disallow data to be added to the database
                  Session["EnableDatabaseProcessing"] = "Disable";

              [Server Side]
              void On_Click_Submit_Button_Save_To_Database_Enent()
                1. Check if the Confirmation checkbox is checked
                   – if not, return error message requesting from the user to enter all required data 
                     and check the confirmation checkbox.

                  if( string.IsNullOrEmpty( (string)Session["EnableDatabaseProcessing"] ) )
                    // The session is not valid, so the current data in the controls may have been added to the database 
                    // but we don’t know. So discard the form data and force the user to enter it again.

                    1.	Uncheck the confirmation checkbox

                    2.	Enable all user data enter controls.

                    Session["EnableDatabaseProcessing"] = "Disable";

                  if( 0 != string.Compare( (string)Session["EnableDatabaseProcessing"],
                                                    StringComparison.InvariantCultureIgnoreCase ) )
                    // The data has been stored in the database
                    Return message that the data has been stored in the database, and ask the user to 
                    uncheck the confirmation checkbox in order to enter the new data.	

                2. Store the data in the database.

                // Record that the data was stored in the database;
                Session["EnableDatabaseProcessing"] = "Disable";

The On-Checkbox-Click event processing function enables and disables the controls as well as the permission for database processing. The On-Click-Submit-Button-Save-To-Database event processing function first checks that the session variable is valid. If the session variable does not have a valid state, then that could be because the session has expired, or depending on the visual controls styles the check box may have not been checked. In either case we require from the user to set/reset and confirm the data first. By confirming the data the database processing variable is set to allow storing the data to the database.

After establishing that the session variable is valid, the On-Click-Submit-Button-Save-To-Database event processing function checks if the database processing is allowed; if not, it simply returns an error message. If it is, the current data entered in the controls is stored in the database, and the database processing variable is set to disallow storing the data to the database. That is all. If the user refreshes the page, they will receive the error message that this data has already been recorded into the database. Using the Forward and Backward arrows is also safe and cannot take the page out of its correct state since the session variable status is changed only after proper form submission by clicking on the "Confirmation" checkbox. Using a session variable with three states is a simple and very effective solution.
Miroslav B. Bonchev
6-th August 2012
London, England
We would love to know your thoughts and opinions on this article. Please leave any comments or questions you may have about it in the box below, and create a free account or subscribe to our newsletter if you wish to be notified when we publish new articles.
Community Content
(To enter your comments you must be signed in. Log in or create FREE account.)
Be the first to comment.
Act On File
Audio Control
Information Presenter
Photo Window
Vat # Validator
Information Presenter
for Museums and Art-galleries
for Hotels, Resorts and Cruises
for Parks of any kind
for Any Business
Encryption and Authentication
Safe Online Communication
Authenticable Website Testimonials
Learn how to store private keys
Make The Most From Your Files
Convenient Volume Control
Photo Window - an Awesome Gift
My Account
FAQ - Forum
Email this page
Buy Now
Public Authentication Key
Public Encryption Key

© Copyright 2020 MBBSoftware. All Rights Reserved.

Email this page
use semicolon to separate emails eg:;
a link to this page will be automatically added to your message
Please type the anti-bot text below.
Type text:
Thank you for subscribing to the MBBSoftware newsletter.
Enter your email address:
Please type the anti-bot text below.
Type text: