How can I ignore an http request without clearing the browser?

Posted by Timid Developer on Stack Overflow See other posts from Stack Overflow or by Timid Developer
Published on 2010-03-24T20:37:53Z Indexed on 2010/03/24 20:43 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

To prevent duplicate requests (i.e. pressing F5 right after clicking a command button), I've setup my page base class to ignore the request if it's detected as a duplicate.

When I say 'ignore' I mean Response.End()

Now I thought I've seen this work before, where there's an issue, I just Response.End() and the users page just does nothing. I don't know the exact circumstance in which this worked, but I'm unable to repeat it now.

Now when I call Response.End(), I just get an empty browser. More specifically, I get this html.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8"></HEAD>
<BODY></BODY></HTML>

I setup the following test app to confirm the problem is not elsewhere in my app. Here it is:

Add the following to an aspx form

<asp:Label ID="lbl" Text="0" runat="server" /><br />
<asp:Button ID="btnAdd1" Text="Add 1" runat="server" /><br />
<asp:Button ID="btnAdd2" Text="Add 2" runat="server" /><br />
<asp:Button ID="btnAdd3" Text="Add 3" runat="server" /><br />

And here's the code behind file

using System;

namespace TestDupRequestCancellation
{
 public partial class _Default : System.Web.UI.Page
 {
  protected void Page_Init(object sender, EventArgs e)
  {
   btnAdd1.Click += btnAdd1_Click;
   btnAdd2.Click += btnAdd2_Click;
   btnAdd3.Click += btnAdd3_Click;
  }

  protected void Page_Load(object sender, EventArgs e)
  {
   if (!IsPostBack)
    CurrentValue = 0;
   else if (Int32.Parse(lbl.Text) != CurrentValue)
    Response.End();
  }

  protected void Page_PreRender(object sender, EventArgs e)
  {
   lbl.Text = CurrentValue.ToString();
  }

  protected int CurrentValue
  {
   get
   {
    return Int32.Parse(Session["CurrentValue"].ToString());
   }
   set
   {
    Session["CurrentValue"] = value.ToString();
   }
  }

  void btnAdd3_Click(object sender, EventArgs e)
  {
   CurrentValue += 3;
  }

  void btnAdd2_Click(object sender, EventArgs e)
  {
   CurrentValue += 2;
  }

  void btnAdd1_Click(object sender, EventArgs e)
  {
   CurrentValue += 1;
  }
 }
}

When you load the page, clicking any button does what is expected, but if you press F5 at any time after pressing one of the buttons, it will detect it as a duplicate request and call Response.End() which promptly ends the task. Which leaves the user with an empty browser.

Is there anyway to leave the user with the page as it was, so they can just click a button?

Also; please note that this code is the simplest code I could come up with to demonstrate my problem. It's not meant to demonstrate how to check for dup requests.

© Stack Overflow or respective owner

Related posts about webforms

Related posts about ASP.NET