Using ASP /Preventing a post action from being duplicated

asked Nov 23, 2012 in General by Maria (1,020 points)
hi there
I have noticed that when a user presses Refresh  on their browser the in my site, the server re-posts the last action. Which is mostly re-posting a comment on a specific post. I am not sure how to fix this ad would appreciate any advice on this.

1 Answer

0 like 0 dislike
answered Nov 23, 2012 by SmartHost (9,060 points)
To solve this you can store a unique code on the client and the server and then compare them on every action request. If both codes are not equal, then the  action would cancel and the page will reload
Add  a Web User Control using this code:
Add a hidden field to hold client code
<asp:HiddenField runat="server" ID="_postcheckcode" />

    protected void Page_Load(object sender, EventArgs e)
    private void CancelUnexpectedRePost()
        string clientCode = _postcheckcode.Value;
  if (!IsPostBack || clientCode.Equals(serverCode))
            string code = Guid.NewGuid().ToString();  
            _postcheckcode.Value = code;
            Session["_postcheckcode"] = code;
You can use this User Control in your master page to  fix  that problem in