No, you cannot call Server.Transfer on an ASP.NET AJAX enabled page

Published Wed, Oct 10 2007 20:24

Even though ASP.NET AJAX has been here for some time, this question still pops up on the forums. So I thought I should put a post here and redirect future questions on the subject to it.

Well, to be precise, what you cannot do is call Server.Transfer (or other transfer method - btw, if you're using IIS 7, there is a new TransferRequest method that might interest you) if you're on  a partial postback (ie, you have an UpdatePanel on your page and, for instance, you're handling the click event of an ASP.NET button that you've dropped inside the UpdatePanel). The reason for this is simple: a partial request is always started by the client PageRequestManager object. This object expects to receive a text response from the server which must follow a specific format (if you're interested in this kind of thing, then just use fiddler to see what's going on) so that it can parse it and change the page DOM according to the changes that have been made on the server side.

Now, if you're calling Server.Transfer, you're really executing the new page (by using the Server.Execute method) and stopping the execution of the current page(ie, the one that has called the Transfer method). This means that the server side of the ASP.NET AJAX platform won't be able to "intercept" the response and change it so that it is sent back on the specific text format that the PageRequestManager object understands. So, in the client side you'll end up getting the HTML generated by the second page. When the PageRequestManager gets the response, it simply doesn't know what to do with it and it ends up generating a parsing exception.

If you need to transfer the user for another page, you can use  the Response.Redirect method since this can be intercepted by the server side of the plaform during a partial postback. When that happens, the "redirect" will be tranformed on a text message that can be understood by the client side, meaning that you'll get your page navigation, instead of the parsing exception you see when you use the Server.Transfer method.

Filed under:

Comments

# Bruno Kenj said on Wednesday, March 12, 2008 1:42 PM

Luis, tudo bem? Eis a solução:

www.brunokenj.net/.../utilizando-servertransfer-e-aspnet-ajax-10

abraços!

# Anz said on Sunday, April 06, 2008 1:48 AM

We can use the cross page posting to make similar functionality of Server.Transfer

here is an interesting post

forums.asp.net/.../1391157.aspx

# Perry said on Friday, May 16, 2008 7:30 PM

I am doing this in the Global.asx.vb:

       Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

           Server.Transfer("Error.aspx")

       End Sub

This works for non ajax errors but it fails for my ajax pages. I need to do a transfer rather than a redirect because I need the context error from the CLR which can not be passed through a querystring that the redirect would need. The other option would be to conditionaly opt out of the Server.Transfer for partial post backs in the global.asx. Anyone know how to tell if we are in a partial post back in the global.asx? There is no page context or script manager there.

# Scott said on Friday, May 30, 2008 12:23 PM

Thanks!  I suspected this, but wasn't sure until I found this post.

Leave a Comment

(required) 
(required) 
(optional)
(required)