2012年3月28日水曜日

Response.Redirect and AsyncPostBackTrigger

I've got a "Previous" button on a webform that uses this code to take user to previous page:

Response.Redirect(Session.Item("PREVIOUSPAGE"))

I set the button as an AsyncPostBackTrigger. This error occurs when the button is clicked:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

If I change the button from AsyncPostBackTrigger to PostBackTrigger, it works fine.

Why won't this button work asynchronously? I've got 2 other buttons on the same page that are being used as AsyncPostBackTriggers with no issue.


I think it would not work because asynchronously - the Ajax enviroment is just expecting to work with the session variables of the current page, but you are trying to tell it to revert back to previous session ids with a redirect. A postback will work because it reloads or mimics a back button of the browser. Not saying it is not possible - I am wrong plenty of times - but it would make sense that that would be the behavior. You'll also get the same behavior if a session times out and the user tries to do a back button to a previous ajax enabled page.

What I would at least consider a better solution is leverage the ability with Ajax to load and unload controls dynamically. This way you can have 'paging' and still retain the benefits of the asyncrhonous enviroment.


There is an article on how to achieve that in the Scott Guthrie's blog:


http://weblogs.asp.net/scottgu/archive/2007/02/06/asp-net-ajax-goodies-documentation-download-back-button-support-new-animation-control.aspx

Juan

0 件のコメント:

コメントを投稿