Home » ASP.NET » Response.Redirect() executes too soon on the Server.

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.

 

Other post in ASP.NET

Related Post

  • ASP.NET Response.Redirect() and JavaScriptASP.NET Response.Redirect() and JavaScript Yesterday we covered issues surrounding using ASP.NET's Response.Redirect in server side code. We noted that not handing it correctly could prevent code from running on the server that we […]
  • jQuery – EventsjQuery – Events As well as being able to change the class associated with an element or a set of elements on a screen, jQuery also allows you to fire events.  You might want to do this, for example, if […]
  • JavaScript bind() for cleaner codeJavaScript bind() for cleaner code Several weeks ago, I wrote about how closures impact calling asynchronous functions in a loop and several ways of dealing with that problem.In my recent coding, I’ve discovered an even […]
  • ASP.NET JSON  and ViewStateASP.NET JSON and ViewState I received the following question recently about my article "ASP.NET AJAX using JSON - Here’s how." If we update the value of a textbox or label via a JSON web service call - will the […]
  • Overriding Events in VB.NETOverriding Events in VB.NET Back in the day, you use to be able to override an event in VB by using the drop down list in the code window.  Today, I had a friend who is moving from Visual Studio 2003 to Visual Studio […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

  • 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.

  • 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/