Monday, February 22, 2010

Response.Redirect cannot be called in a Page callback

If you are getting the “Response.Redirect cannot be called in a Page callback” then you are most probably using AJAX and you are using a Response.Redirect (or Server.Transfer) in an event that was fired from within an AJAX panel.

The reason this does not work is that there is only a partial post-back that occurs with controls that are within an AJAX panel and you cannot perform a Redirect from within a partial post-back.

Fixes:

1. Dont do a redirect in an event that is fired due to a partial post-back!

2. In the event, instead of doing a redirect, register a javascript method that will perform the redirect from the client’s browser. eg:

string script = string.Format("document.location.href = '{0}');", "pageToRedirectTo.aspx");
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "redirect", script, true);

1 comment:

Fabão said...

Hi, Raj.
I think "document.location.href = '{0}';" is better than "document.location.href = '{0}');"
;)

Nice post, anyway!

Regards
Fabio