Question

Does a Response.Redirect first PostBack to the page that's calling the redirect before actually redirecting? I don't think so but my debugging is showing me that it is.

I can see it doing so and that causes me a problem. The problem is this:

  1. My page has an asp.net button on it.
  2. When the button is clicked, the page posts back of course. Then the following is called in the event handler method for that button:

    Response.Redirect("Checkout?frpp=1"), true);
    
  3. But I notice that when the redirect is called, it first goes back and hits the page load for this page, the page that's doing the redirecting. Well that is causing me problems because it's calling code that I don't want it to be calling again such as this:

    if (!IsPostBackl) ShowOrderItems();

So it's like I'm doing a 2nd PostBack:

  1. First PostBack happens when you click the button, hits the event handler, runs my handler method which calls the Redirect.
  2. Redirect is called but it's doing another PostBack to the same page here..why? Is that possible?
Was it helpful?

Solution

Response.Redirect doesn't cause a PostBack - it issues an HTTP 302 response, which causes the browser to issue a request to the new URL.

Response.Redirect does finish the current function and call stack that it's in though - it doesn't automatically abort the current thread. Perhaps that's what you're seeing?

OTHER TIPS

That is the way ASP.Net buttons work. They post back the page. When this happens, the page load event runs first (well, actually, some internal framework stuff, and then page init, and then page load). After this, your button click handler will run.

It seems to me you have answered your own question - the "if (!IsPostBack)" is the thing to use to skip running the page load code on a post back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top