问题描述
对于每个 WebForms 开发人员来说,这可能是一个简单的问题,但我对这个场景很熟悉.我有一个页面上有一个添加"按钮,它会导致一个 jquery 弹出窗口,用户可以在其中提交一个表单,该表单将向页面上的转发器添加一个条目.我的问题是,在用户添加项目后,如果他们点击刷新按钮,项目将再次添加.我无法通过 page.ispostback 阻止它,因为这会阻止原始添加.我可以在这里做什么?
This is probably a simple question for every WebForms developer but I am knew to this scene. I have a page that has an "ADD" button on it that results in a jquery popup where the user can submit a form that will add an entry to the repeater on the page. My problem is that after the user adds an item, if they then hit the refresh button the item will get added again. I can't stop this with the page.ispostback because that would block the original add. What can I do here?
推荐答案
这是一个常见问题.这是问题的解释和解决方案.
that's a common problem. Here's explanation and solution of the problem.
当通过 HTTP POST 请求将 Web 表单提交到服务器时,Web 用户尝试在某些用户代理中刷新服务器响应可能会导致重新提交原始 HTTP POST 请求的内容,从而可能导致不希望的结果,例如重复的网络购买.为了避免这个问题,许多 Web 开发人员使用 PRG(Post/Redirect/Get) 模式.
When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase. To avoid this problem, many web developers use the PRG(Post/Redirect/Get) pattern.
从维基复制(LINK)
最简单的解决方案可以是Response.Redirect 到同一页面(即,如果您的页面名为 default.aspx
,请写入 Response.Redirect("default.aspx")
).如果您执行此浏览器刷新按钮,则只会加载页面,就像您输入地址栏 URL 并导航到该页面一样.
simplest solution can be Response.Redirect to the same page (i.e. if you page is named default.aspx
write Response.Redirect("default.aspx")
). if you do this browser refresh button will just load the page as if you have typed in address bar URL and navigated to it.
这里是 SO 问题 如何停止不需要的回发,这也可能有用.
here's SO question How to stop unwanted postback that might be useful as well.
这篇关于Webforms 刷新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!