当用户点击浏览器的后退按钮prevent回传 [英] Prevent Postback when user clicks browser's back button

查看:94
本文介绍了当用户点击浏览器的后退按钮prevent回传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,发送电子邮件至多个用户(联机通讯组列表)。在点击提交按钮和发送电子邮件后,状态页面显示在清单多少封电子邮件被送往,错误等信息。如果用户单击后退按钮,电子邮件是发送。我怎样才能prevent呢?

I have a web page that sends email to multiple users (online distribution list). After the submit button is clicked and the email is sent, a status page is shown listing how many emails were sent, errors, and other information. If the user clicks the back button, the email is resent. How can I prevent this?

请注意:浏览器不会提示用户实际发送电子邮件之前为重新提交或重新发送数据页面,但不会从点击它停止我的用户,然后不知道为什么邮件的两个副本被送往出去。

NOTE: The browser DOES prompt the user to "resubmit" or "resend" data to the page before actually sending the email, but that does not stop my users from clicking it and then wondering why two copies of the email were sent out.

环境:


  • 服务器:C#,ASP.NET 2.0,IIS6

  • 客户端:任何浏览器(我不希望像一个SmartNavigation的IE-具体的解决方案)

推荐答案

在您的code,其中电子邮件的发送点之后,做302重定向到一个确认页面:

In your code, right after the point where e-mails are sent, do a 302 redirect to a confirmation page:

protected void btnSend_Click(object sender, EventArgs e)
{
    SendManyEmails();
    Response.Redirect("confirmation.aspx");
}

通过这种code时,POST原来的页面将不会在浏览器的历史结束了。

With this kind of code, the POST to the original page will not end up in the browser history.

这常见的模式就是所谓的邮政/重定向/获取模式

This common pattern is known as the Post/Redirect/Get pattern.

该模式的主要缺点是,当重定向用户从POST请求的处理所有的状态都失去了 - 于是,开始一个新的请求上下文。在ASP.NET这包括存储在<$的和所有控制对象中的成员,以及一切C $ C>的ViewState 。

The main drawback of this pattern is that all state from the handling of the POST request is lost when redirecting the user - thus, commencing a new request context. In ASP.NET this includes members within the Page and all Control objects, as well as everything stored in the ViewState.

如果你产生某种状态对象的 - 也许日志发送的邮件 - 在处理POST请求,你会需要一些方法来保存这个对象为以下GET请求。一些Web框架具有功能专门针对这样的:回报率有闪存,ASP.NET MVC有的TempData 。 ASP.NET表单已经没有这样的概念内置的,所以你必须想办法的自己。

If you generate some kind of "status object" - maybe a log of sent mail messages - while handling the POST request, you will need some way to save this object for the following GET request. Some web frameworks has functionality specifically for this: RoR has flash, ASP.NET MVC has TempData. ASP.NET forms has no such concept built in, so you will have to figure something out yourself.

保存对象到会话的POST,阅读和删除它下面的GET将是解决这个问题的方法之一。你可以建立一个抽象围绕这个,如果你在几个地方使用它,或者你可以在网上搜索现有Flash实现/ TempData的用于ASP.NET表单。

Saving the object to the Session on the POST, reading and deleting it on the following GET would be one way to solve this. You can build an abstraction around this if you use it in several places, or you could search the web for existing implementations of flash/TempData for ASP.NET forms.

这篇关于当用户点击浏览器的后退按钮prevent回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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