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?
The three most popular methods (which are often used in tandem) are:
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.
Check out this ASP.NET AJAX control called PostBack Ritalin from a fellow SO'r Dave Ward.
I have solved the problem writing a javascript disabling the click function button:
Even i got the Same Problem I have resolved like Below.
After uploading a File If you Redirect to
same page
orsome other page in your project
this problem will be avoided.For Example:
In My ASPX
Even i got the Same Problem I have resolved like Below.
After uploading the File If you Redirect to same page or some other page in your project. After Redirection Response will not be there once you redirected.
In My ASPX
In My Code Behind