Click here to Skip to main content
13,350,289 members (47,179 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 25 Jan 2012

How to prevent Re-Post action caused by pressing browser's Refresh button

, 5 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Simple Web User Control prevents unexpected action caused by Refresh Button

The Problem

When a user presses the Browser's Refresh button (Or F5/Ctrl+R etc.) the browser resends the last request data to the server.
This action causes an Unexpected Re-Post action (if there was one).
In some cases, it can be very problematic (For example, in shopping website when the user clicks on "Add to Cart" button and then presses the Refresh button - the item will be added twice !!!)

The solution

We will store a unique code (in my case Guid) on both client and server and compare them on every request action. If the two codes are not equal, the action will be canceled and the page will reload ('GET' method).

Add Web User Control with this code:


Add a hidden field to hold client code
<asp:HiddenField runat="server" ID="_repostcheckcode" />

Code behind

protected void Page_Load(object sender, EventArgs e)

private void CancelUnexpectedRePost()
    string clientCode = _repostcheckcode.Value;

    //Get Server Code from session (Or Empty if null)
    string serverCode = Session["_repostcheckcode"] as string  ?? "";

    if (!IsPostBack || clientCode.Equals(serverCode))
        //Codes are equals - The action was initiated by the user
        //Save new code (Can use simple counter instead Guid)
        string code = Guid.NewGuid().ToString();
        _repostcheckcode.Value = code;
        Session["_repostcheckcode"] = code;
        //Unexpected action - caused by F5 (Refresh) button

Now you can drag the User Control to every page you want to handle that problem, or you can drag it to your Master Page to handle the problem in the entire site.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Udi Perets
Team Leader
Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionMy vote 5 Pin
Millone23-Jan-15 9:04
memberMillone23-Jan-15 9:04 
QuestionUpdate Panel Timer activation causes full repost Pin
ScotSunnergren8-Jul-14 12:01
memberScotSunnergren8-Jul-14 12:01 
QuestionVote 5 Pin
Guna14319-May-14 19:35
memberGuna14319-May-14 19:35 
GeneralMy vote of 5 Pin
niubay5-Sep-12 23:29
memberniubay5-Sep-12 23:29 
GeneralMy vote of 4 Pin
SurangiT27-Aug-12 20:59
memberSurangiT27-Aug-12 20:59 
Member 91711282-Aug-12 2:10
memberMember 91711282-Aug-12 2:10 
Questionprevent Re-Post action by pressing browser's Refresh Pin
Michael Chiew27-Jul-12 20:03
memberMichael Chiew27-Jul-12 20:03 
QuestionSuggestion Pin
Jens Meyer12-Mar-12 7:21
memberJens Meyer12-Mar-12 7:21 
GeneralReason for my vote of 5 i realy needed this one , greate pos... Pin
uri bahbut8-Feb-12 2:58
memberuri bahbut8-Feb-12 2:58 
GeneralReason for my vote of 5 i realy need that , very good one !!... Pin
uri bahbut8-Feb-12 2:55
memberuri bahbut8-Feb-12 2:55 
GeneralReason for my vote of 2 what happens when a user has more th... Pin
enoreth5-Feb-12 5:28
memberenoreth5-Feb-12 5:28 
GeneralReason for my vote of 5 Good tip Pin
adribusc1-Feb-12 1:17
memberadribusc1-Feb-12 1:17 
GeneralReason for my vote of 5 Nice Tip Pin
ThatsAlok31-Jan-12 22:05
memberThatsAlok31-Jan-12 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 5 Feb 2012
Article Copyright 2012 by Udi Perets
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid