Hitting refresh causes click event to fire
I have this piece of code:
<asp:Button runat="server" Text="Process Payment" />
When I click on button the page refreshes which is fine. But now when I press F5 on browser my click event again gets fired and I can get hit my breakpoint. Now problem is this button is contacting payment gateway in it's click event handler. Pressing refresh sends the request again to the payment gateway. Please help. What should I do?
Redirecting to the same page solves the problem. However I don't want t开发者_开发百科o do that. Is there any other better alternative?
This is because you refreshing a page that has been posted. This is the same thing that happens when you press the back button and the browser warns you about posting the same form again.
The only way to avoid this, outside of using JavaScript to handle your post, is to redirect to the same or another page after you complete the processing.
The only other way I know of is to use AJAX. We use ajaxified Telerik controls. The server-side code fires, but without a full postback, so we can refresh the page without reposting.
In other parts of our app, we will use the Response.Redirect method which you don't like. I don't like it either.
Also, here's a link that could help:
http://www.codeproject.com/KB/aspnet/SyncControl.aspx
AJAX is the way to go in a situation like this. It's one of the reasons why it's so popular in payment systems.
精彩评论