简体   繁体   中英

On hitting refresh of the web page , Button1_Click event is fired again

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM