Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (22 votes)
5 Feb 2012CPOL 83K   41   16
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:

Markup

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

Code behind


C#
protected void Page_Load(object sender, EventArgs e)
{
    CancelUnexpectedRePost();
}

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;
    }
    else
    {
        //Unexpected action - caused by F5 (Refresh) button
        Response.Redirect(Request.Url.AbsoluteUri);
    }
}


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.

License

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


Written By
Team Leader
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMulti-tab experience? Pin
stamminator19-Sep-19 9:35
stamminator19-Sep-19 9:35 
QuestionWorked for me Pin
Member 230073021-Feb-19 9:07
Member 230073021-Feb-19 9:07 
QuestionIt does not work for me Pin
Member 109731836-Sep-18 19:01
Member 109731836-Sep-18 19:01 
QuestionMy vote 5 Pin
Millone23-Jan-15 8:04
Millone23-Jan-15 8:04 
QuestionUpdate Panel Timer activation causes full repost Pin
ScotSunnergren8-Jul-14 11:01
ScotSunnergren8-Jul-14 11:01 
QuestionVote 5 Pin
Guna14319-May-14 18:35
Guna14319-May-14 18:35 
GeneralMy vote of 5 Pin
niubay5-Sep-12 22:29
niubay5-Sep-12 22:29 
GeneralMy vote of 4 Pin
SurangiTi27-Aug-12 19:59
SurangiTi27-Aug-12 19:59 
QuestionURL REDIRECTION Pin
Member 91711282-Aug-12 1:10
Member 91711282-Aug-12 1:10 
Questionprevent Re-Post action by pressing browser's Refresh Pin
Michael Chiew27-Jul-12 19:03
Michael Chiew27-Jul-12 19:03 
QuestionSuggestion Pin
Jens Meyer12-Mar-12 6:21
Jens Meyer12-Mar-12 6:21 
GeneralReason for my vote of 5 i realy needed this one , greate pos... Pin
uri bahbut8-Feb-12 1:58
professionaluri bahbut8-Feb-12 1:58 
GeneralReason for my vote of 5 i realy need that , very good one !!... Pin
uri bahbut8-Feb-12 1:55
professionaluri bahbut8-Feb-12 1:55 
GeneralReason for my vote of 2 what happens when a user has more th... Pin
enoreth5-Feb-12 4:28
enoreth5-Feb-12 4:28 
GeneralReason for my vote of 5 Good tip Pin
Adriano Buscema1-Feb-12 0:17
Adriano Buscema1-Feb-12 0:17 
Reason for my vote of 5
Good tip
GeneralReason for my vote of 5 Nice Tip Pin
ThatsAlok31-Jan-12 21:05
ThatsAlok31-Jan-12 21: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.