Web Hosting Geeks | Web Hosting Experts
Need help? Call us 1(866)TOP-HOST

14611 reviews 1734 hosting providers

Questions & Answers

Using ASP /Preventing a post action from being duplicated

Nov 23, 2012 by Maria
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.
Thanks.

1 Answer

0 votes
Nov 23, 2012 by SmartHost
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)
    {
        CancelUnexpectedRePost();
    }
 
    private void CancelUnexpectedRePost()
    {
        string clientCode = _postcheckcode.Value;
 
        
  if (!IsPostBack || clientCode.Equals(serverCode))
        {
        
            string code = Guid.NewGuid().ToString();  
            _postcheckcode.Value = code;
            Session["_postcheckcode"] = code;
        }
        else
        {
           
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
You can use this User Control in your master page to  fix  that problem in