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 Daniel [Sep 06, 2008 @ 5:04 AM]
Hi Joel,
How do I detect a new tab in Internet Explorer?
I have an ASP.NET page which hosts my application. I can detect a new session for a user through session_start in global.asax. I can detect a refresh though the server page_load event. But to detect a new tab, I have few possible options to try out:
1) identify HTTP_Connection ?
2) count the static objects in Session ?
3) Response.IsClientConnected ?
4) HTTP_REFERER ?
I don't know if any of these will do it. Please help!!
Thanks

post reply to this comment Comment by massage shanghai [Dec 21, 2008 @ 5:08 PM]
massage shanghai,???[url=http://www.shanghaimassage9.com]massage shanghai[/url]???????,???massage shanghai??????,??????massage shanghai????,,????massage shanghai,?????,??????massage shanghai????J??????Massage table????????[url=http://www.massageoem.com/en/Products_list.asp?classid=1]Massage table[/url]???,Massage table??·,????Massage table???????????,?????Massage table?????Z?????????[url=http://www.massageoem.com]Massage table[/url]??????????????Massage table???,????U????Massage table??????????????????????Massage table????????????[url=http://www.wholesalenikesneaker.com/product.asp?Classid=50&Nclassid=513&i=1]max 90[/url]???????????max 90??????????max 90?????max 90??????max 90??????max 90??
????[url=http://www.topsofts.com/office_tools/221.html]microsoft office[/url](?????????)????????????j?microsoft office????????????????microsoft office??????microsoft office??????microsoft office??????microsoft officeMifare card????,[url=http://www.e-plasticcard.com/Mifare_card.htm]Mifare card[/url]???????,Mifare card??????,????Mifare card????,????Mifare card??????,????Mifare card???????mobile software???,[url=http://www.topsofts.com/internet/mobile/73/82.html]mobile software[/url]???,mobile software???,mobile software????,??mobile software,??mobile software,mobile software????,??mobile software,???mobile software,mobile software???
???moblie download???? ????[url=http://www.topsofts.com/internet/mobile/73/82.html]moblie download[/url]??? ???moblie download???? ???????moblie download ??????moblie download
post reply to this comment Comment by dvd ripper [Mar 16, 2009 @ 11:30 PM]
????dvd ripper???[url=http://www.topsoftwareol.com/product/DVD/DVD_Ripper.html]dvd ripper[/url]ý?????ý????dvd ripper????dvd ripper????dvd ripper??????dvd ripperý????ý????[url=http://www.topsoftwareol.com/products/dvd-ripper.html]dvd ripper[/url]??????dvd ripper?????????dvd ripper??????????????dvd ripper?????????????dvd ripperdvd ripper for mac????. [url=http://www.topsoftwareol.com/product/MAC/DVD_Ripper.html]dvd ripper for mac[/url]???????????dvd ripper for mac; dvd ripper for mac????????. ???dvd ripper for mac??? ???[url=http://www.topsoftwareol.com/product/MAC/DVD_Ripper.html]dvd ripper mac      [/url]????????????dvd ripper mac      ??????,??????dvd ripper mac      ý??dvd ripper mac      ????dvd ripper mac      ?????dvd ripper mac      ???º?dvd ripper mac      ?????????[url=http://www.topsofts.com/dvd_video/16.html]dvd software[/url]???????dvd software???ID??IP, dvd software?????????I????? .... dvd softwareg??. - dvd software?????????dvd software??g????[url=http://www.topsofts.com/dvd_video/16.html]dvd software [/url]ó?????????????dvd software ?????????????????????dvd software ???????????dvd software ??????ecosway?????|???[url=http://www.ekosv.cn]ecosway[/url]????????????ecosway??ecosway????????Adjobs.com.cn??????ecosway?????????[url=http://www.andarm.net]Electro Shocker[/url](?????????)????????????j?Electro Shocker????????????????Electro Shocker??????Electro Shocker??????Electro Shocker??????Electro Shocker
post reply to this comment Comment by Ess Dee [May 31, 2009 @ 6:46 PM]
this is good.
post reply to this comment Comment by avi converter for mac [Jul 27, 2009 @ 9:31 PM]
???????[url=http://www.dvdsoftwares.de/product/DVD_to_AVI_Converter_for_Mac.html]avi converter for mac [/url]????. ???avi converter for mac ????. ???avi converter for mac ????. ???avi converter for mac ??????? ???????avi converter mac2007????????[url=http://www.topdvdsofts.com/Products/MAC/DVD_to_AVI_Converter.html]avi converter mac[/url]?????????? ... ?????avi converter mac??? .... ?havi converter mac?????????avi converter mac?????????avi converter mac?????????[url=http://www.567software.com/Products/mac/DVD_to_AVI_Converter_for_mac.html]avi converter mac[/url]?????????avi converter mac???,??????avi converter mac???,avi converter mac????????????avi converter mac??????[url=http://www.dvdsoftwares.de/product/DVD_to_AVI_Converter_for_Mac.html]avi converter mac[/url]???????????avi converter mac????? ????????????????avi converter mac?????????????avi converter mac?????????avi converter mac???????????[url=http://www.dvdsoftwares.net/Products/MAC/DVD_to_AVI_Converter.html]avi converter mac[/url]????????????avi converter mac?????avi converter mac??·??????avi converter mac???????avi converter mac???????¡?avi converter mac?????[url=http://www.dvdsoftwares.de/product/DVD_To_AVI_Converter.html]avi dvd converter[/url],avi dvd converter???,????avi dvd converter??,avi dvd converter??????,avi dvd converter?????,??????AVI iPhone Mac??????[url=http://www.dl32.com/product/iPhone/TOP_AVI_to_iPhone_Converter.html]AVI iPhone Mac[/url]?????????AVI iPhone Mac????????AVI iPhone Mac????????????AVI iPhone Mac???????????????AVI iPhone Mac???????????????AVI iPhone Mac????????[url=http://www.dvdsoftwares.de/product/DVD_To_AVI_Converter.html]avi to dvd converter[/url]???u?????????????avi to dvd converter?????????
post reply to this comment Comment by bearking [Nov 03, 2009 @ 12:47 AM]
[url=http://www.xilisoft.com/blu-ray-to-dvd-suite.html]blu-ray to dvd[/url]
post reply to this comment Comment by bearking [Nov 03, 2009 @ 12:52 AM]
Xilisoft iPod Rip permits you to rip, copy or backup your iPod music, videos, photos, podcasts and audio book to PC or the other way around. You can even sync songs between your iTunes 9.0 and you iPod Rip software.
post reply to this comment Comment by beaking [Nov 03, 2009 @ 12:55 AM]
Xilisoft iPhone Transfer is a smart tool for synchronizing your iPhone to your computer.
post reply to this comment Comment by fds [Nov 10, 2009 @ 5:28 PM]
<a href=http://www.springcare.cn>baby education</a>
post reply to this comment Comment by nhim [Nov 17, 2009 @ 12:58 AM]
In 1427, after 10 years of war, <a href="http://miaikan.co.jp/">?? ??</a> Vietnam regained its independence and China <a href="http://miaikan.co.jp/">????? ??</a> officially acknowledged Vietnam as an independent state. Lê L?i took the throne and was declared Emperor of Dai Viet  <a href="http://miaikan.co.jp/">?????????</a> (??) (though King is a more accurate term for the ruler of Vietnam)
post reply to this comment Comment by dvd ripper [Dec 09, 2009 @ 11:43 PM]
leawo dvd ripper is the best dvd ripper
Leave Your Comment
Name:
Email:  (gravatar enabled)
URL:
Comment:
or Cancel