Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent repeated postbacks from confusing my business layer

I have a web application (ASP.Net 3.5) with a conventional 3 layer design. If the user clicks a button a postback happens, some middle and data layer code runs, and the screen is refreshed. If the user clicks the button multiple times before the first postback is completed my logic gets confused and the app can end up in an invalid state. What are the best ways to prevent this?

I can use javascript to disable the button but this just hides the problem. How do I build my business and data layers to handle this?

like image 963
Sisiutl Avatar asked Jan 26 '09 22:01

Sisiutl


2 Answers

The three most popular methods (which are often used in tandem) are:

  1. Disable submit buttons once clicked/pressed;
  2. Use POST+REDIRECT+GET to avoid back button issues; and
  3. Replace history in the browser so you can't go back (but this should be used sparingly and with good reason).
like image 146
cletus Avatar answered Nov 15 '22 04:11

cletus


If I was to be brutally honest, I would say that it sounds like you're the one confused about web postbacks, not your application (that's if you're the one who wrote it). ;-)

That said, in addition to other suggestions, what I would do in this case is place a "token" in hidden field in the form - like a GUID - that is posted back. Use this to track the work being done and only allow it to be used once. E.g. when posted back, place it in session storage. Each time a postback is performed check the session first for this token, and if it is there then do nothing. If it's NOT there, store it in session and do the work. When the session ends, tokens are thrown away automagically. Easy. Much better than some convoluted database token.

  • Oisin
like image 28
x0n Avatar answered Nov 15 '22 03:11

x0n