Preventing re-execution of method when press browser's refresh button

Often we see that after executing a functionality by a button click (through posting a form to server), when we click on browser refresh button then a pop up re-sending previous page information to server shows and after clicking resend (retry in IE) that particular functionality re-executed i.e reinsertion occurs in database or some other functionality that previously had been done.

So how to prevent this problem in programming (i.e not execution previous functionality again.)
Just copy and paste function given below any where in CS page and register a hidden field to ASPX page

public bool notIsRefresh()
    bool retVal = false;
    if (Session["match"] == null || Session["match"].ToString() == hdnRFMatch.Value)
        retVal = true;
    Session["match"] = DateTime.Now.Ticks;
    hdnRFMatch.Value = Session["match"].ToString();
    return retVal;

<%--Register anywhere in page--%>
<asp:HiddenField runat="server" id="hdnRFMatch" />

notIsRefresh is a bool return type function which return true if page posted directly by the button given in the page and return false if page posted through the browser refresh button.

And further use like this below,

 protected void btnInsert_Click(object sender, EventArgs e)
     if (notIsRefresh())
         //Response.Write("not refresh");
         //Do Your work here.

Recent Posts