Home | Software | WebLog | Contact | Wish List

Refresh Detection (Asp.NET)

Overview

Many websites suffer from the form browser refresh problem. For example, let's say you have a shopping cart. After the user buys item X, the page displays an invoice. The user refreshes the page, which asks if he'd like to repost the form. The user clicks "Retry" and the form is submitted again, charging his credit card and adding a duplicate order.

To fix this problem I have created a control (inspired by Dino Esposito's browser refresh detection code) that can be simply slipped into every page that may suffer from the form repost problem.

If you found this or any other code on this site usefull and want to show your appreciation, now you can. Thanks!

Example

Below is an example; a button that posts a form. (Most forms have text box fields, but for this example they are not necessary and have been omitted).

Page Status: This page has NOT been refreshed.
Button Status: This button has been clicked 0 times.



Click the button and press your browsers refresh button to test the refresh detection.


My contact form now detects browser refresh to eliminate multiple submissions. Please feel free to test it out and send me an email :)

Visual Studio Toolbox Installation

Add the control to your toolbox. Go to Tools > Customize Toolbox. Click the tab .Net Framework Components. Click on Browse, locate Joel.Net.Refresh.dll and click ok. You should now see "Refresh" in the toolbox.

post reply to this comment Comment by Darin [Jul 15, 2004 @ 9:50 PM]
Know where I could get my hands on that refresh dection source code? Thanks, Darin
post reply to this comment Comment by Alexander Popov [Apr 05, 2005 @ 4:26 AM]
It's not full protection. 1. open page 2. duplicate it ( ctrl-n ) 3. press Button on first ( OK ) 4. press Button on second. "This page HAS been refreshed." message appears ( Bug ) Did you know any techique to prevent this behavior?
post reply to this comment Comment by jon [Dec 26, 2005 @ 10:17 PM]
hi Nice code :)
post reply to this comment Comment by khirod kumar [Mar 17, 2006 @ 3:51 AM]
hi,
Very good source.
post reply to this comment Comment by Mahmoud Tahoon [May 14, 2006 @ 2:53 AM]
your code is just great, it did help me .. thank you very much.
post reply to this comment Comment by Shah bhavik [Dec 19, 2006 @ 10:27 PM]
I understand your src code. but In the src code Refresh1 is defined and it is used in the binary file.
so please give me the proper source code.

Your code is great.
Thanks
post reply to this comment Comment by rishi [Jan 15, 2007 @ 2:05 AM]
My contact form now detects browser refresh to eliminate multiple submissions.
here hyper link on "contact form" is not working
post reply to this comment Comment by Tester [Feb 04, 2007 @ 11:22 PM]
There is a but with this code.  When you first come to this page, click on the "Clicke Me" button to so the button status is "..clicked 1 times".  

Now leave this page by using the navigation to the left (click on the cookie detection link).  Once one the cookie detection page, use the navigation again and click on the Refresh Dection link.  Notice that when you return back to Refresh Dection page, it thinks the page was refreshed.  

If this is not a bug, please let me know why this occurs.
post reply to this comment Comment by Amit [Mar 01, 2007 @ 8:13 PM]
I have 8 pages in my application. Starting from 2nd page I have your control on each page to detect page refresh. However when the user navigates from 1st page to 2nd page, the property IsRefreshed of control is set to TRUE from the start. Is this a bug?

Thank you.
post reply to this comment Comment by Leendert Halter [Jun 11, 2007 @ 1:36 AM]
Trying to install the Refresh control I encounter the following problem in VS2005 (working with VM ware):
1. SelectTools/Choose Toolbox Items menu
2. Select tab .NET Framework Components
3. browse and select Joel.Net.Refresh.dll yields error message:
"Invalid URI: The hostname could not be parsed". Why is this and what can I do to solve this?
The host name is: amsdc1-s-6133.europe.shell.com
post reply to this comment Comment by Aries [Aug 13, 2007 @ 7:33 PM]
Hey, I found a bug here. Copy this URL: http://joel.net/code/refresh_capture.aspx and then click on any other link and browse away from this page. Once you are in a different page, paste the above URL and it will say: The page HAS been refreshed. I have a similar problem, can't seem to get away with it.
post reply to this comment Comment by Joseph Humphrey [Sep 09, 2007 @ 8:33 PM]
Based on this example I have rewritten the code to work with ajax and can be used as a base page in the App_Code folder.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DomPage
/// </summary>
public class DomPage : Page
{
    bool _isrefresh = false;
    string _tname = "__Ticket";

    /// <summary>Increments ticket and checks the last ticket served.  All the work is here.</summary>
    public DomPage()
    {
        this.PreInit += new EventHandler(DomPage_PreInit);
    }

    void DomPage_PreInit(object sender, EventArgs e)
    {
        // Change null to 0 (prevents errors)
        if (Session[_tname + "_LastServed"] == null || Request[_tname] == null) { Session[_tname + "_LastServed"] = 0; }
        // Get current and last ticket served
        int ticket = Convert.ToInt32(Request[_tname]) + 1;
        int lastticket = Convert.ToInt32(Session[_tname + "_LastServed"]);

        // Refresh detection
        if (ticket > lastticket || (ticket == 0 && lastticket == 0))
            Session[_tname + "_LastServed"] = ticket;
        else
            _isrefresh = true;
    }

    /// <summary>Outputs hidden field with ticket id.</summary>
    /// <param name="output">Html Text Writer</param>
    protected override void Render(HtmlTextWriter output)
    {
        //Checks if the page is using Ajax
        if (ScriptManager.GetCurrent(this) != null)
            ScriptManager.RegisterHiddenField(this, _tname, Session[_tname + "_LastServed"].ToString());
        else
            Page.ClientScript.RegisterHiddenField(_tname, Session[_tname + "_LastServed"].ToString());

        base.Render(output);
    }

    /// <summary>Returns true if page has been refreshed, otherwise false.</summary>
    public bool IsRefresh
    {
        get { return _isrefresh; }
    }
}
post reply to this comment Comment by Joseph Humphrey [Sep 09, 2007 @ 8:35 PM]
You may want to remove the html breaks.. The comment added those :)
post reply to this comment Comment by ??? [Jul 03, 2008 @ 6:11 PM]
??????[url=http://www.hztyzs.cn]???[/url]???? ?? ????????????? ?? ?????_??????? ?? ?????_??????? ?? ??i???????? ?????a?????????????,??h?????[url=http://www.sjglj.com/cpzs_cpfl.asp]????????[/url]??(Beijing traveling),??????????????·,??????????????????·,??????????????·,????????????·,?????????????h???????[url=http://www.sjglj.com/cpzs_cpfl_juti_datu.asp?id=300]???????[/url]???????????ý???????????????????????????????????????????????????????????????????????????????[url=http://www.sabg.com.cn]?????[/url]???????????????????????????????????????????????????????????????????[url=http://www.sino-office.com]??¥????[/url]??????¥????????????¥??????????¥?????????????¥??????¥?????????????¥?????????[url=http://www.sino-office.com]??¥????[/url]???????????????????u?¥????,???j?¥????,?????¥??????????
post reply to this comment Comment by ??????????????? [Jul 08, 2008 @ 11:05 PM]
??????????????????-[url=http://www.hzqs.com.cn/www/42/2008-01/794.html]???????????????[/url]??????,?????,?????,??????,???????????????????,???????????????????,???????????????????????????????? ????????????[url=http://www.hzqs.com.cn/www/42/2008/3142.html]??????????????? [/url]???????????????????? ??????????????????? ???????????l?????????????? ???š???????????????? ??????????????????? ??????????????????? ????????
???????????? ,??????????????? ??,[url=http://www.hzqs.com.cn/www/42/2008/3116.html]???????????? [/url]??,??????????????? ,??????????????? ,???,???[url=http://www.hzqs.com.cn/www/42/]?????????[/url]????,?????????????,??????,??????,????????????,???????????,????????????
???[url=http://www.hzqs.com.cn/www/42/2008-01/783.html]?????????????[/url]??????????????????????ID??IP, ??????????????????????I????? .... ?????????????g??. - ????????????????????????????????????g????w??ë???[url=http://www.hzqs.com.cn/www/2/2008-01/1080.html]?????????ô???? [/url]?????????????l????????ô???? ??????????l????????ô???? ???????????ô???? ???????????L??l????????ô???? ???????
Leave Your Comment
Name:
Email:  (gravatar enabled)
URL:
Comment:
or Cancel