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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/aspnet/251482.html

猜你在找的asp.Net相关文章