[英]How to prevent firing of last event after page refresh?
Every time I refresh the browser, my button's event handler fires again. 每次刷新浏览器时,按钮的事件处理程序都会再次触发。 How do you prevent this?
您如何防止这种情况?
One way could be to use an HTTPModule. 一种方法是使用HTTPModule。 You can generate a client side GUID, using Javascript, and have that posted back to your server.
您可以使用Javascript生成客户端GUID,并将其重新发布到服务器上。 Then, if the user refreshes, you compare that GUID and see if it already exists on your server.
然后,如果用户刷新,则可以比较该GUID,并查看它是否已存在于服务器上。 If it does exist, then it's a refresh and you shouldn't perform your button event handler's method.
如果确实存在,那就是刷新,并且您不应该执行按钮事件处理程序的方法。 An example of this implementation is described in this blog post .
此博客文章中描述了此实现的示例。
Another idea could be to redirect the page (or redirect it to itself) after the user has pressed the button. 另一个想法是在用户按下按钮后重定向页面(或将页面重定向到自身)。 This would give a clean postback and refreshing it shouldn't submit the event again.
这将提供一个干净的回发,并且刷新它不应该再次提交该事件。
I always try to use the PRG (Post-Redirect-Get) pattern, even though it seems a little bit unnatural to the Web Forms system. 我总是尝试使用PRG (后重定向获取)模式,即使对于Web Forms系统似乎有点不自然。
This basically means after getting a Button_Click event, you do a redirect, perhaps using QueryString parameters to indicate to the resulting page what to display. 这基本上意味着,在获取Button_Click事件后,您可以进行重定向,也许使用QueryString参数向结果页面指示要显示的内容。
If you are not doing partial page updates with UpdatePanels, you don't 'lose' much doing it that way, anyway. 如果您不使用UpdatePanels进行部分页面更新,那么无论如何您都不会“丢掉”这种方法。 (and if you are using partials, refreshing the page wouldn't necessarily repost anyway!)
(如果您使用的是局部页面,则刷新页面不一定会重新发布!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.