简体   繁体   English

在刷新 web 页面时,再次触发 Button1_Click 事件

[英]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.当我单击提交按钮时,会触发 button1 事件并提交数据。

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.我不明白为什么即使在我点击刷新并且我无法点击 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.这里发生了什么:然后刷新页面,浏览器发送与之前发送的请求完全相同的请求,因为您发布了它发送 POST 到服务器的表单。 Generally, same form should never be submitted twice, so browser (not only IE) gives a warning for that.一般来说,同一个表单永远不应该提交两次,所以浏览器(不仅是 IE)会为此发出警告。 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.这是很正常的行为,但是,在您处理 POST 的所有时间,您都必须检查您是否没有两次发布相同的数据。 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://aspalliance.com/687
http://msdn.microsoft.com/en-us/library/ms379557%28VS.80%29.aspx 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.codersource.net/asp-net/asp-net-articles/working-with-post-back-in-asp-net.aspx
http://www.knowdotnet.com/articles/asppagerefresh.html http://www.knowdotnet.com/articles/asppagerefresh.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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