Ajax enabled webpart not working on second postback

Trying to develope an Ajax webpart with aspx.net 2.0 I came up with this msdn example  which seemed to be exactly what I was looking for. I followed the steps suggested by the article but I ended up with some weird behaviour from the webpart when finally deployed in my Sharepoint server.

Apparently everything was working as expected and when I clicked the webpart the first time, the ajax method was correctly executed and the “Hello! XXX” text succesfully displayed. However, clicking again the button didn’t seem to do anything, also, other page actions that should submit the form and do some action (like the “Edit Page” item from the “Site Actions” menu) stopped working.

I finally found that the problem was in the EnsurePanelFix function of the example which was not working as expected

private void EnsurePanelFix()
{
   if (this.Page.Form != null)
   {
     String fixupScript = @"
     _spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
     function _initFormActionAjax()
     {
       if (_spEscapedFormAction == document.forms[0].action)
       {
         document.forms[0]._initialAction =
         document.forms[0].action;
       }
     }
     var RestoreToOriginalFormActionCore =
       RestoreToOriginalFormAction;
     RestoreToOriginalFormAction = function()
     {
       if (_spOriginalFormAction != null)
       {
         RestoreToOriginalFormActionCore();
         document.forms[0]._initialAction =
         document.forms[0].action;
       }
     }";
   ScriptManager.RegisterStartupScript(this,
     typeof(SayHelloWebPart), "UpdatePanelFixup",
     fixupScript, true);
   }
}

So what I did is to provide my own implementation of that function. What we needed is to invalidate the onSubmit wrapper that Sharepoint calls in order to ensure some callback scenarios, this can be done by setting to true the _spSuppressFormOnSubmitWrapper variable so I replaced the original function by the following implementation

private void EnsurePanelFix()
        {
              ScriptManager.RegisterStartupScript
                (this,
                 typeof(AjaxEnabledWebpart),
                 "UpdatePanelFixup",
                 "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;",
                 true);
}

And that’s it ! my Ajax webpart is working properly no matter how many times we click the Hello World ! button :-)

About these ads

12 Responses to Ajax enabled webpart not working on second postback

  1. [...] The only relevant code from the AjaxEnabledWebpart is the CreateChildControls method, which creates the UpdatePanel control and calls the virtual method CreatePanelControls passing as a parameter the control collection where child pages are supposed to add the controls. Also, you can find the EnsurePanelFix implementation on my previous post at this link [...]

  2. [...] Ajax enabled webpart not working on second postback [...]

  3. Sreenath says:

    Thank you so much. My two days of seach for this problem ended here. I was trying to use some ajax controls on an asp.net page which was part of a custom workflow.

  4. John Scott says:

    Thanks. Fantastic job.

    FYI… This is working for a custom ASP.NET page that we are integrating in SharePoint. So it works for more than just webparts.

    Excellent!!!

  5. Same here, this solution works for a custom layout page. Many thanks =)

  6. Stark Botha says:

    OMG! Thanks!

    such an elegant solution.

  7. Manh Le says:

    OMG THanks , You’re cheese’nhee-us ,I would never know this.
    Thanks that solved my problem.
    It Seems to me Microsoft never posts the appropriate Solutions. This is the second time I follow Microsoft MSDN article, End up with Errors and Wierd stuff.

  8. Adam says:

    I try this sample. It doesn’t work in moss SP1. Updatepanel not refresh accordingly at all. It only work without SP1. Is it this sample test in moss SP1? Any idea?

  9. Pankaj says:

    I am using Ajax model pop extender. Using a Link button i try to open the pop up window. After doing some fuctionality in POP up i need that link button will be disabled. It is disabling first time. But second post back it still remains enable. If any body has some solution than please suggest. Thanks in advance.

    Pankaj

  10. Gostixel says:

    Хм,согласен с предыдущими блоггерами
    ^..^ :-)

  11. Mike Sheridan says:

    Thanks so much for this solution. I spent alot of time trying to figure this one out. Now my code works.

  12. Rajkumar says:

    hey ur my god. ur solution help me a lot.
    thanks dude
    thank u very much.

    hats-off to u.

    regards
    Raj

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: