Reputation: 12735
I have a simple webform which have few text fields,leabels and Submit button. When I click on Submit button then button1 event is fired and data is Submitted.
But what worries me is , when I refresh the page after submitting data immediately and then I get the following errror:
And when I see the database to check the entry there there exist a duplicate entry.
I don't understand why the button click even is being fired even when I hit refresh and there is no way I clicked on Button1.
And how do I avoid this from happening.
Upvotes: 2
Views: 1916
Reputation: 99
This is a natural behavior. One of the easiest way to avoid this is to do a redirect to the same page after the post.
Upvotes: 1
Reputation: 19821
What is happening here: then you refresh the page, browser send exactly the same request as it send previously, since you posted the form it sends POST to server. Generally, same form should never be submitted twice, so browser (not only IE) gives a warning for that. Since you confrim that it re-send same form and you code executed second time.
This is quite normal behavior, but! All the time you handle POST, you have to check that you do not post same data twice, this is serious error. You can handle that either on bussiness logic lever or DB level, doesn't matter. But it should be handled.
Upvotes: 4
Reputation: 1844
When you press the refresh button it will be sending the request to get the current page that was initiated before by the button click event.
You can get more help from the following links.
http://aspalliance.com/687
http://msdn.microsoft.com/en-us/library/ms379557%28VS.80%29.aspx
http://www.codersource.net/asp-net/asp-net-articles/working-with-post-back-in-asp-net.aspx
http://www.knowdotnet.com/articles/asppagerefresh.html
Upvotes: 0