English 中文(简体)
Prevent Postback when user clicks browser s back button
原标题:

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.

Environment:

  • Server: C#, ASP.NET 2.0, IIS6
  • Client: any browser (I don t want an IE-specific solution like SmartNavigation)
最佳回答

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");
}

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.

Bonus info about keeping state when doing Post/Redirect/Get

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.

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.

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签