如何避免在页面的刷新按钮事件 [英] How to avoid the button events on Refresh of page

查看:220
本文介绍了如何避免在页面的刷新按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.aspx页面,页面插入数据上的按钮,点击数据库。但是,当我preSS的按钮,它会正确。 I M获得成功的消息为成功插入数据。在这种情况下,如果我preSSF5或刷新它发射按钮点击事件的页面。为什么它应该是什么?如何避免这种情况?

I have .aspx page that page inserts the data to the database on a button click. But when i press the button it is going right. i m getting the Successfully message as " successfully inserted data". In this situation if i press "F5" or Refresh the page it is firing the button click event. Why it should be ? How to avoid this condition ?

推荐答案

当用户点击<大骨节病> F5 (或使用工具栏按钮刷新页面),它的将会的原因一个新的请求,相同于previous之一,要被发送到服务器。在 Button.Click 活动的将会的再次被提出,但你必须保护自己不受两次插入数据的几种方法。

When the user clicks F5 (or uses a toolbar button to refresh the page) it will cause a new request, identical to the previous one, to be sent to the server. The Button.Click event will be raised again, but you have a few ways to protect yourself against inserting the data twice.

最好的办法,恕我直言,是使用邮政/重定向/获取模式。在您的code,其中的数据被保存在点之后,做302重定向到一个确认页面:

The best way, IMHO, is to use the Post/Redirect/Get pattern. In your code, right after the point where the data is saved, do a 302 redirect to a confirmation page:

protected void btnSaveStuff_Click(object sender, EventArgs e)
{
    SaveStuffToDatabase();
    Response.Redirect("confirmation.aspx");
}

在使用该模式时, POST 原来的页面在浏览器历史不会结束,并刷新结果页面会导致最终 GET 被重复,这应该是安全的。

When using the pattern, the POST to the original page will not end up in the browser history, and refreshing the result page will cause the final GET to be repeated, which should be safe.

这篇关于如何避免在页面的刷新按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆