微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我从事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.

原文地址:https://www.jb51.cc/aspnet/251482.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐