简体   繁体   English

如何防止刷新页面后触发上一个事件?

[英]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.

相关问题 Gridview Change事件未触发,插入记录后如何刷新网格? - Gridview Change event not firing and how to refresh the grid after inserting a record? 如何在以编程方式与控件交互后防止事件触发? - How to prevent event firing after programmatically interacting with controls? 防止在页面刷新时重复执行上一个动作 - Prevent last action repetition on page refresh 阻止按钮单击事件以刷新页面 - Prevent a button click event to refresh the page 如何防止自动触发RadioButtonList indexchange事件? - How to prevent auto firing RadioButtonList indexchange event? 从列表中删除项目后如何防止SelectedIndexChanged事件在ListBox中触发 - How to prevent SelectedIndexChanged event from firing in ListBox after removing an item from it 如何在以编程方式移动鼠标后防止触发 MouseMove 事件 - How to Prevent a MouseMove Event from Firing After the Mouse has been Programmatically Moved 防止在C#中刷新页面中的button_click事件 - Prevent button_click event in page refresh in c# 如何在回发时阻止页面刷新? - how to prevent page refresh while doing postback? 以编程方式检查控件时如何防止触发 CheckedChanged 事件? - How to prevent firing CheckedChanged event when checking a control programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM