加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何阻止不必要的回发

发布时间:2020-12-16 00:07:06 所属栏目:asp.Net 来源:网络整理
导读:我从事ASP.NET C#.在按钮点击事件下我想保存它工作正常,但按下浏览器的刷新按钮后,再次发生此事件我想停止此事件. 解决方法 关于这个主题的文章. Preventing Duplicate Record Insertion on Page Refresh 方法1 A simple solution is to Response.Redirect b
我从事ASP.NET C#.在按钮点击事件下我想保存它工作正常,但按下浏览器的刷新按钮后,再次发生此事件我想停止此事件.

解决方法

关于这个主题的文章.

Preventing Duplicate Record Insertion on Page Refresh

方法1

A simple solution is to
Response.Redirect back to the same
page after the INSERT command is
called. This will call up the page
without transmitting any post headers
to it. Using Request.Url.ToString()
as the first parameter of
Response.Redirect will cause both the
URL and the page’s querystring to be
included in the redirect. The use of
false as the second parameter will
suppress the automatic Response.End
that may otherwise generate a
ThreadAbortedException. A
disadvantage of this approach is that
any ViewState that had been built up
will be lost.

方法2

一种相关的方法是将表单提交到中间处理页面然后将Response.Redirect返回到调用页面,类似于表单处理的传统ASP方法.这与仅使用Button_Click事件中的Response.Redirect具有相同的效果,因此它具有相同的缺点,另外的缺点是为网站开发人员创建另一个页面来管理.

方法3

The next batch of solutions works by
determining whether the user has
refreshed the page in the browser
instead of pressing the form’s submit
button. All of these solutions depend
on the ability of the website to use
Session variables successfully. If
the website uses cookie-based
Sessions,but the user’s browser does
not permit the use of cookies,these
solutions would all fail.
Additionally,should the Session
expire these solutions would also
fail.

方法4

Should the user somehow manage to circumvent the above mentioned solutions described above,the last line of defense is at the database. There are two methods that can be employed to prevent a duplicate record from being inserted into the database. For each method,I’ve moved the SQL code into a stored procedure,since there are now more processing steps involved and these are easier to illustrate in a separate stored procedure. Note however that a stored procedure is not strictly required in order for these methods to work.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读