Saturday, February 12, 2011

.NET AJAX 1.0 Aysc Callback Modifies Form Action When Server.Transfer is Used

I have a web form that I am attempting to implement dynamic drop down lists on using the .NET AJAX 1.0 extensions. I have successfully implemented the needed bits, but have an interesting quirk.

When I select a value from my first drop down list, my call back happens and my page is updated correctly. The next value I select, I receive the following error:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned was: 404

Reguarless of what control I use first, the first request works and the second does not. Looking at my IIS logs, I see the following lines:

2008-10-17 14:52:14 W3SVC1 127.0.0.1 POST /Aware/Xtend/mParticipant/NewPlannedService.aspx WIN=Participant_1224255079212&Title=Participant 80 - 127.0.0.1 200 0 0

2008-10-17 14:52:20 W3SVC1 127.0.0.1 POST /Aware/mParticipant/NewPlannedService.aspx WIN=Participant_1224255079212&Title=Participant 80 - 127.0.0.1 404 0 0

As you can see my post URL has completely changed. Using Fiddler to watch the request/response, I can see this in the response from the server:

|formAction||NewPlannedService.aspx|

This is simply the name of the page that is being executed, the relative path and query string has been dropped off.

I can resolve this issue by adding the following to the end of my Async callback method:

this.Form1.Action = Request.Url.PathAndQuery

But this seems incredibly lame and smells somewhat like moldy cheese to me. Can any one point me in the right direction?

UPDATE: Upon further inspection I discovered that NewPlannedService.aspx was not the original executing page. Page1.aspx was executing and then called Server.Transfer("/folder/NewPlannedService.aspx"). So the URI in the browser was http://whatever.com/Page1.aspx, but the actual page that was executing was http://whatever.com/folder/NewPlannedService.aspx

  • To solve this issue, I created a javascript file called Ajax.Server.Transfer.Fixer.js with the following code:

    var orginalFormAction = null;
    
    //capture the current form action value
    function BeginRequestHandler() {
      orginalFormAction = theForm.action;
    }
    
    //set the form action value back to the
    //correct value
    function EndRequestHandler() {
      theForm.action = orginalFormAction;
      theForm._initialAction = orginalFormAction;
    }
    
    function RegisterRequestHandlers() {
    
      if (typeof (Sys) != "undefined") {
    
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
        Sys.Application.notifyScriptLoaded();
    
      }
    }
    
    //register request handlers after the application 
    //has successfully loaded.
    Sys.Application.add_load(RegisterRequestHandlers);
    

    Then added the following line to my Page_Load event:

    protected void Page_Load(object sender, EventArgs e)
        {
          PageScriptManager.Scripts.Add(
            new ScriptReference("~/Script/Ajax.Server.Transfer.Fixer.js")
            );
        }
    
    From NotMyself

0 comments:

Post a Comment