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

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

问题描述

我有.aspx页面,该页面将数据插入到数据库的按钮上。但是当我按下按钮的时候,它就是正确的。我将成功的消息作为成功插入数据。在这种情况下,如果我按F5或刷新页面,它正在点击按钮单击事件。为什么应该是如何避免这种情况?

解决方案

当用户点击 F5 (或使用工具栏按钮刷新页面),它将导致与先前相同的新请求发送到服务器。 Button.Click 事件将再次提起,但您有几种方法来保护自己免于两次插入数据。



嗯,最好的方式是使用 Post /重定向/获取模式。在您的代码中,紧接着保存数据的位置,请执行302重定向到确认页面:

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

使用模式时, POST 到原始页面不会最终在浏览器历史记录中,刷新结果页面将导致最后的 GET 重复,这应该是安全的。 / p>

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 ?

解决方案

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.

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");
}

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天全站免登陆