Jaggu
Jaggu

Reputation: 6428

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 to do that. Is there any other better alternative?

Upvotes: 2

Views: 1655

Answers (3)

Dot NET
Dot NET

Reputation: 4897

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.

Upvotes: 2

oscilatingcretin
oscilatingcretin

Reputation: 10919

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

Upvotes: 3

Charles Graham
Charles Graham

Reputation: 1157

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.

Upvotes: 4

Related Questions