Jarrod Everett
Jarrod Everett

Reputation: 801

Response Redirect on Postback

My code looks like:

protected void Page_Load(object sender, EventArgs e)
{
   if(IsPostback)
   {
      if(/* User is not authenticated to perform any actions on this page */)
      {
        Response.Redirect(/* Error Page */);
      }
   }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
   /* do some stuff */
}

I would have guessed that if I submit the button to do a postback and I was not authenticated, then in the Page_Load the redirect would happen and would send back an HTTP 302. However, it appears that it runs the Response.Redirect line of code, keeps processing past it in Page_Load, and then even runs btnSubmit_Click afterwards. The response is not the error page.

I can put a return call after the Response.Redirect to prevent further processing in that function, but the btnSubmit_Click function will still get run.

Why would it work this way?

Upvotes: 2

Views: 8665

Answers (1)

Garrett Vlieger
Garrett Vlieger

Reputation: 9494

To prevent more code from processing, use this:

Response.Redirect(url, true);

The second parameter will end further processing instead.

Upvotes: 6

Related Questions