btnAdd_Click火灾时,我preSS F5 [英] btnAdd_Click fires when i press F5

查看:160
本文介绍了btnAdd_Click火灾时,我preSS F5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个简单的code,以将数据插入到按钮单击事件分贝。它正在执行罚款以及将数据插入数据库。插入数据,如果我preSS F5键它再次插入新行到数据库使用相同的值后。

Hi I've a simple code to insert data into db in button click event. It is executing fine and insert data into db. After inserting data if i press f5 button it is again inserting a new row into db with the same values.

上的每个按钮F5按钮单击事件触发(刷新)

Button click event fires on each f5 button (refresh)

笏问题吗?

感谢您,
纳古

Thank you, Nagu

推荐答案

这是有意设计的。当你preSS F5你重做你只是做了这是一个回发。以prevent这一点,你有解决方案,如无尽的量:

This is as designed. When you press F5 you are redoing what you just did which was a postback. To prevent this you have an endless amount of solutions such as:

这是初始加载使用一个会话变量,一旦它被张贴在第一时间(保存),清除值。请确保您检查此值presence。

Use a session variable on initial load and once it is posted the first time (saved), clear the value. Make sure you are checking for the presence of this value.

// On inital page load for data entry store a key.
// This is a basic example, you should have a session wrapper
Session["Unsaved"] = 1;

// When you do the save logic make sure you check this session variable and save if
// it still exists.
if (Session["Unsaved"] != null)
{
  // Save data here
  Session.Remove("Unsaved");
}
else
{
  // Show message that save has already completed or session has expired.
}

您也可以做检查DB侧并验证你是不是插入相同的数据(可能无法正常工作,因为它可以在你的情况下有效)

You could also do the check DB side and verify that you are not inserting identical data (which may not work as it could be valid in your scenario)

在过去,我已经通过存储版本号,并具有其提交的修订版本号与发布的数据与编辑为例pvented此$ P $。保存时,如果版本号不匹配(因为它是在每次更新改变,检查是DB方),那么更新失败,​​我会显示一条消息。

In the past I have prevented this with edits for example by storing a revision number and having it submit the revision number with the posted data. When saving, if the revision numbers don't match (since it is changed with each update and the check is done DB side) then the update fails and I would display a message.

有关新的数据,你也可以做一个Response.Redirect的一个完整的网页这使他们成功/失败的最新信息。如果他们点击刷新,他们只会被重新加载完整的页面,而不是页面是做插入/更新。

For new data, you could also do a Response.Redirect to a 'complete' page which gives them updated information of success / failure. If they hit refresh, they will just be reloading the completed page and not the page that was doing the insert / updating.

再有这么多不同的解决方案。没有什么是完美的每一个场景,所以你需要找出最适合你。

Again there are so many different solutions. Nothing is perfect for every scenario so you will need to find out which works best for you.

这篇关于btnAdd_Click火灾时,我preSS F5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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