Server.Transfer in an UpdatePanel

by Arnold Matusz 6 10 2008

While writing a short piece of code for my global exception handling post I figured out a nice fact. What I wanted to achieve is to have redirection to a custom error page which displays exception details. This can be easily done by using Response.Redirect() (to ex: ~/exception.aspx"), but this will completely lead to another page. This is not usefull if I'm interested in the actual URL where the exception has been thrown. (ex: ~/products.aspx?catid=4&pageid=7 ..."). By using Response.Redirect() you end up with "/exception.aspx" as the URL in the browser.

An obvious solution at first sight is ... we need to use Server.Transfer() which won't create a new Context (i.e.: the url in the browser will stay the same but we show the contents of a completely new page). And of course this is all fine ... until you need to use this functionality in an ASP.NET AJAX enabled website in an UpdatePanel.

By calling Server.Transfer() during an asynchronous postback the following alert will be displayed:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '
<!DOCTYPE html P'.

The reason why you can't use a Server.Transfer() in an UpdatePanel is more and more obvious when you think about what AJAX is. We only request small pieces of markup which replace little portions of a page, the whole point of AJAX is to avoid downloading the whole page again. This means that when triggering an async postback, the PageRequestManager expects to receive some small portion of HTML, but because the Server.Transfer() call was made the actual response is the markup for a completely new/different page (which begins with the DOCTYPE definition, this is what you can see at the end of the exception message).

If you decide to use a Server.Transfer() in an UpdatePanel you can avoid this exception by setting your Trigger to a PostBackTrigger. (i.e. The control which fires the method where Server.Transfer() is called won't cause an async postback!).

DZone it Digg it Submit to StumbleUpon Submit to Technorati Submit to reddit.com Submit to del.icio.us Submit to NewsVine Submit to Furl Submit to BlinkList

Tags: , , ,

AJAX | ASP.NET | Programming

Comments

10/15/2008 2:41:21 AM #

trackback

Trackback from DotNetKicks.com

Server.Transfer() in an UpdatePanel

DotNetKicks.com |

6/4/2009 2:24:40 AM #

Noah

Thanks for the warning.  Now I know how to use triggers.

Noah United States |