Thursday, July 12, 2007

AJAX and Server.Transfer battles 2° round

Yesterday, as I said in the previous post, I was fighting with some interesting features of ASP.NET Ajax 1.0. My situation was:

I had two ASP.NET pages, Page1 and Page2 configured as follow:

  •  Page1 acts as the Web Site home page
  •  Page2, in a different dircetory, acts as a chid, content page

Page1, redirects, via Server.Transfer, to child pages as Page2. Page2 contains an UpdatePanel and some DropDownLists controls.

Here is the problem, when Page2 take control, the "ajaxed" controls works fine the first time, but next time it doesn't. The exception throwed was:

Sys.WebForms.PageRequestManagerErrorException: unknown error ocurred while processing the request on the server. The status code returned from the server was: 404

So after hours spent looking for a solution, an example, or anything else could help me, I found this post http://forums.asp.net/p/1078772/1788445.aspx, the situation explained is the same as above.

The problem I experienced was that Page2 after load, mantains the correct form action, but after the first call then, the action became incorrect and no actions could be performed. So I tried to find a solution in many ways:

  1. searching if the client-page-content remains the same between the first well-done call and the second one
  2. because the ajaxed dropdown was outside the UpdatePanel, I tried to modify the related UpdatePanel Trigger

After I tried many solutions, I found very helpful the solution provied in the post. The solution is to add a Client-Side Javascript script to attach a custom function to the PageRequestManager to handle the EndRequest. There change the form action so the action remains the same across any ajax content refresh and any postback.

this is the code:

<script type="text/javascript">
<!--//
function EndRequestHandler()
{
theForm.action =
"../Folder1/Page2.aspx";
theForm._initialAction = theForm.action;
}
if( typeof(Sys) != "undefined" )
{
EndRequestHandler();
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
//-->
</script> 

Because I spent so much time over this, I think this could be helpful and useful to others, without spending time to looking for a solution!

Ciao!

Technorati tags: ,

No comments: