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 !!!)
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 ('
Add Web User Control with this code:
Add a hidden field to hold client code
<asp:HiddenField runat="server" ID="_repostcheckcode" />
protected void Page_Load(object sender, EventArgs e)
private void CancelUnexpectedRePost()
string clientCode = _repostcheckcode.Value;
string serverCode = Session["_repostcheckcode"] as string ?? "";
if (!IsPostBack || clientCode.Equals(serverCode))
string code = Guid.NewGuid().ToString();
_repostcheckcode.Value = code;
Session["_repostcheckcode"] = code;
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.