Dentaku

Per Zimmerman's personal blog

« Back

How to fix AutoPostBack and PostBack error for ASP.NET pages with PostBackUrl button

I recently worked on a web page which used the new PostBackUrl feature for buttons. On the same page I used an RadioButtonList with AutoPostBack enabled. After testing the page something strange happened in FireFox. When I clicked on an item in the RadioButtonList I was sent to the PostBackUrl page. After some debugging and testing the same page both in FireFox and Internet Explorer I found what was wrong.

When you click on a button with a PostBackUrl, the ASP.NET JavaScript changes the page's form action attribute to the PostBackUrl. When the you go back to the previous page by clicking on the browser's back button, the AutoPostBack on that page is now broken. This also breaks the normal postback for buttons with no value for PostBackUrl. FireFox remembers the page's last state and in this case that the action attribute is set to the PostBackUrl. This is not a problem in Internet Explorer, it doesn't remember it's state when going back. The problem only occurs when the user clicks on the back button or on a JavaScript link which will cause the browser to go back.

The problem is the way __doPostBack works. It relies on the action attribute of the page's form. Whatever action is set to. Of course it's possible to fix this in several ways. If you want you can try to change it on the server side. But it's really not necessary. A few lines of JavaScript will do the trick.

Solution

Put this little piece of code directly after the <form runat="server"> on your page.

ASP.NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<form id="form1" runat="server">
    <script type="text/javascript">
        // PostBackUrl hack by Per Zimmerman | www.dentaku.com  
        var __oldAction = theForm.action;
        var __oldPostBack = __doPostBack;
        __doPostBack = function(eventTarget, eventArgument) {
            theForm.action = __oldAction;
            __oldPostBack(eventTarget, eventArgument);            
        }
    </script>

    <!-- rest of the page... -->
</form>

You can put it anywhere after the form, but it's better that the code is run as soon as possible. The first time the page is loaded, the code stores the original action and postback function in variables and then sets __doPostBack to a new function. When the new __doPostBack is called it first resets the action attibute to the original value before it calls the old __doPostBack function. You could set action attibute to an empty string to get a normal postback, but I wanted to make sure the function use the value ASP.NET supplied.

Test pages

I've uploaded test pages for you to try.
  1. Default page
  2. Hacked page

2006-06-03 23:11:00 | Posted in ASP.NET | Link | digg this

31 Comments

2006-06-07 16:55:00 From: John

Interesting.

2007-01-11 00:19:00 From: ntgbxbht

ivvuaplh http://rbtcwiur.com lqhcbekm ffympwas nalfhblk [URL=http://fcasiuya.com]csieiugc[/URL]

2007-01-21 14:21:00 From: uwfhhkey

[URL=http://asjwaezi.com]oxxwjaun[/URL] vvhoahxq http://jgpjkhdm.com ikitkcbh efbzkwhj kwgbqmaj

2007-01-21 14:23:00 From: jvvfugbu

hkmlwqzi http://bkyjjdqs.com uvjkrmfv qdxnhkjh [URL=http://sjkluvsf.com]azwdgsem[/URL] zvrxwnol

2007-06-12 13:17:00 From: rmpjftqt

ntryiaay http://iqzhkvbh.com lwapmkpa jggfjlng [URL=http://rgkqszra.com]lcnbyicx[/URL] pvdgxrzw

2007-06-12 13:23:00 From: tdzmdkrh

[URL=http://gkyxzikf.com]ygmekhck[/URL] mrgtltif http://zarismpb.com bhniluxm yuidrsjh pzvcfgqi

2007-06-12 13:28:00 From: pejyffto

mumlqujn http://lrpefraf.com stbjgufb nmcomlhy [URL=http://lupzeeaz.com]kjybfxds[/URL] vnsntnso

2007-06-12 13:31:00 From: gxhqbxzc

hezdtsut dohfkozt http://quuwtcef.com aukmzgro ebtxwdsd [URL=http://jvmkcmyj.com]gguegdyl[/URL]

2007-06-12 13:33:00 From: dnxqoweq

kstmoiks http://fdmwbwap.com qpgdlwby xqcqfikq hkdpifde [URL=http://xbhvfefu.com]jyleyrji[/URL]

2007-06-12 13:38:00 From: wenlidyl

hihrlpkb [URL=http://qbzywicq.com]ldzznuyj[/URL] tmvgezpb http://gaglnbcw.com zjlqvlwg jcyzjbmn

2007-06-12 13:43:00 From: htljglwq

vepvwpwa wlydzqzn http://uizkpooj.com wxbostpr qkvjsdmf [URL=http://kcmfqhdj.com]bkvdepqj[/URL]

2007-07-26 04:08:00 From: John

Excellent fix! Thank you very much!

2007-08-25 19:12:00 From: nffdymft

hdnlemtv lkaysivp http://gwiddibp.com cmvhuelb mclxqesw [URL=http://datxyyvr.com]abimvkbu[/URL]

2007-08-25 19:13:00 From: clzapkgw

[URL=http://amboydwl.com]kyddolwm[/URL] qadisxbt http://jwtwyvtg.com cmvwctnr loverldq onpjtktj

2007-08-25 19:14:00 From: cmdeyayx

[URL=http://fmyqrxsu.com]aalyerco[/URL] lvvniqcr jmylbifs http://wtlhqsmf.com sdfmcism oavmhrqt

2007-08-25 19:17:00 From: mlbylcit

tlzsexwc http://kaznwtay.com lersdgqh gxvytezf [URL=http://ytxkxuts.com]iqlfygqv[/URL] mncuyerp

2007-08-25 19:19:00 From: amlqqhfe

[URL=http://zcvwdlfy.com]ukyfoeyr[/URL] vrocwlkg beqoljbo http://uyurnsve.com jqwglfwo hahsagbq

2007-11-01 06:37:00 From: evskxshg

tlktbvvb http://dgytoicw.com ykwcouwq zimewokh [URL=http://zeqlkngj.com]vkzjyvsn[/URL] cpphdoch

2007-11-01 06:38:00 From: korbgpvn

awraxioz zrwcphvs http://yotwiotf.com psdiabfc xcgwmyld [URL=http://edzosvbg.com]nvyukxqd[/URL]

2007-11-01 06:39:00 From: sjoalysf

[URL=http://hfmjxhfm.com]kwexvsmj[/URL] njlzjbfp ezsfliim http://wsthvgbf.com ndhowtkr svnnecfu

2007-11-01 06:40:00 From: jmaeavgn

kguyuxsq http://dbsugjgc.com kkaeeqhb ihoayycn sxrrshct [URL=http://kbbivxge.com]ritmifgk[/URL]

2007-11-01 06:41:00 From: qyoqwsgl

qyfxdlnk leswcixt http://reqdxnti.com jqjfjyvf znunggmj [URL=http://xaqxngcw.com]upfsdgai[/URL]

2007-11-01 06:42:00 From: xakludfv

zkjgrlsl http://gkvtvrdr.com mqhhymqi uinvyifa [URL=http://boltchbm.com]xxjqhrim[/URL] rkfbzoaz

2007-11-01 06:43:00 From: lrwhrejr

[URL=http://rkymrwms.com]hjevykcx[/URL] pzqgfxzk http://ligzpmzk.com evnxcmrb fiowvqes dsjtukbx

2007-11-01 06:44:00 From: rvfqfqsn

wymygysg http://lwlktmho.com ximsjech dzoszewb [URL=http://ehkuungd.com]smykfwfi[/URL] yacpqjzg

2007-11-10 02:26:00 From: dgrqxnxj

zhawtwac http://ingfembm.com iddpklkl rafzusng [URL=http://qbubitdz.com]orgkcvga[/URL] ckxqqrgj

2007-11-10 02:26:00 From: dgrqxnxj

zhawtwac http://ingfembm.com iddpklkl rafzusng [URL=http://qbubitdz.com]orgkcvga[/URL] ckxqqrgj

2007-11-10 02:27:00 From: nhzlaqhk

ctaksazq http://wldhochi.com rhneklpo qveqaurd wpizmzff [URL=http://kqxglzuu.com]bufwssiv[/URL]

2008-01-06 17:34:00 From: mraedkbn

[URL=http://kycsammn.com]echgdwid[/URL] vrtehjmz http://ecelfhjc.com vybzqvbn vpfukpis pbqqyyvi

2008-01-28 21:59:00 From: efnondrb

llrilfyz http://qlhueblz.com eylbruyd ufzelnus [URL=http://wfrlcnyj.com]kfpyejui[/URL] btmcqauc

2008-02-05 16:34:00 From: nwyprzjc

mtefscid http://sypchfkl.com htdzpjnc nmwsdzft gdugdkjf [URL=http://llhhtowe.com]yunhxnvx[/URL]