Response.Redirect() executes too soon on the Server.

tp_vol4_017 I’ve seen this question a couple of times in various situations. The first involves Javascript and the second involves server side code. Both are caused by a misunderstanding of what this function does and how web pages work.

Let’s start with the easy one: server side code.

You might have code that looks something like this:

protected void Page_Load(object sender, EventArgs e)
{

    // Do something here
    Response.Redirect("~/newpage.aspx");

    // do some more code here

}

The problem with this code, which is probably obvious to most of you, is that the “do some more code here” section will never fire because we’ve done a redirect right before it.

But is this as obvious?

protected void Page_Load(object sender, EventArgs e)
{

    // Do something here
    Response.Redirect("~/newpage.aspx");

}
protected void Button1_Click(object sender, EventArgs e)
{
    // do some more code here

}

The problem here is that Page_Load is the first event to fire. Button1_Click() will never execute because we’ve done the redirect during our page load.

Aside from the fact that you should not perform form processing code during Page_Load(), the other issue is that events can fire in any order. So maybe you were smart and processed your form in a Button.Click event handler, but have you accounted for the fact that your databinding code may need to put data in the database AFTER you run Response.Redirect()?

There is a way around this.

Response.Redirect() has two overloads. The first, which we are all very used to, is the one I’ve shown above. But all it does is call the second which has two parameters, the URL and a boolean value, to indicate if we should stop processing right away and return to the browser. The default that gets sent in with the first overload is TRUE. Go ahead and quit.

By modifying our code to:

protected void Page_Load(object sender, EventArgs e)
{

    // Do something here
    Response.Redirect("~/newpage.aspx",false);

}
protected void Button1_Click(object sender, EventArgs e)
{
    // do some more code here

}

we can be sure that our Button1_Click method, or databinding code, will be executed.

Tomorrow we’ll look at the issues involved with Javascript.

Related Post

  • Response.Redirect() and JavaScriptResponse.Redirect() and JavaScript Yesterday we covered issues surrounding using Response.Redirect in server side code. We noted that not handing it correctly could prevent code from running on the server that we want to run. T...
  • When Session Objects Get Created With No Session VariablesWhen Session Objects Get Created With No Session Variables I thought about calling this  Session Object Madness, but it really isn’t that crazy once you think through what’s happening. Here’s the issue.  I have a client who does work for anot...
  • DotNetNuke Response.End() Doesn’t EndDotNetNuke Response.End() Doesn’t End OK.  This one is just plain annoying! For some reason that I’ve yet to be able to track down, Response.End() seems to have no impact at all under DotNetNuke. I tried various hacks.  Including...
  • Delegates in .NETDelegates in .NET I received the following question: What is a delegate?  What problem does it solve?  and When might I use a delegate? A delegate is essentially a function pointer.  We have used function poi...
  • OOP is not the main problem w/ ASP.NETOOP is not the main problem w/ ASP.NET Over at Jeff's Junk, Jeff discusses what he feels is the main problem people have with ASP.NET.  That is, they don't understand object oriented programming. Jeff, you've got half the problem....
  • Bryce

    I looked FOREVER online to figure out how to make my form submit to the db before doing the redirect and this handles the problem in two seconds! Thanks for the great post.

  • http://jeffmartin.com Jeff Martin

    I realize this is example code, but one wonders how you would ever get to that page to even display the button to click if it redirects at every load…

  • Dave

    You are right, it wouldn’t. in “real” code you’d probably have an

    if(IsPostback)

    around the redirect.

  • Amis

    Well.. I tried using the overload. But my problem is that I cannot use a button to cause postback.

    I have a

    //Do something here — a Javascript

    Response.Redirect(arg1,false);

    Even with this, my Javascript doesnt fire.

    Any solution?

  • Dave

    You want the second half of this topic which you an find here http://blog.dmbcllc.com/2009/01/28/responseredirect-and-javascript/