Andornot Consulting
Monday, July 09, 2007 11:23 AM

ASP.NET AJAX and Sys.Webforms.PageRequestManagerServerErrorException

by Ted Jardine

Using ASP.NET AJAX extensively in my latest project I've been sporadically running into the Sys.WebForms.PageRequestManagerParserErrorException. It got to the point that I was contemplating ripping out ASP.NET AJAX completely until this known issue had been ironed out. The various causes for this error are mentioned many different places, but for some samples, go here, here, and here.

Quoting from Eilon Lipton's blog posting, this particular exception is very common and can be caused by any one of the following:

  1. Calls to Response.Write():
    By calling Response.Write() directly you are bypassing the normal rendering mechanism of ASP.NET controls. The bits you write are going straight out to the client without further processing (well, mostly...). This means that UpdatePanel can't encode the data in its special format.
  2. Response filters:
    Similar to Response.Write(), response filters can change the rendering in such a way that the UpdatePanel won't know.
  3. HttpModules:
    Again, the same deal as Response.Write() and response filters.
  4. Server trace is enabled:
    If I were going to implement trace again, I'd do it differently. Trace is effectively written out using Response.Write(), and as such messes up the special format that we use for UpdatePanel.
  5. Calls to Server.Transfer():
    Unfortunately, there's no way to detect that Server.Transfer() was called. This means that UpdatePanel can't do anything intelligent when someone calls Server.Transfer(). The response sent back to the client is the HTML markup from the page to which you transferred. Since its HTML and not the special format, it can't be parsed, and you get the error.

The problem was I wasn't doing any of the above (who uses Response.Write in an ASP.NET application these days?) and I was still sporadically encountering the error - a show stopping error I might add. An error that is popped up in a javascript warning box completely undecipherable to the end user leaving an empty/useless/castrated UpdatePanel in its wake. This of course leaves the end user feeling likewise empty/useless/castrated (to say nothing of the developer).

This post here indicates that there is a problem with the RoleMangerModule or any time you set a cookie to the response in an AJAX callback, which can only be solved by doing one of the following:

  1. Disable caching of cookies in the RoleManager. (yuck)
  2. Handle the Application's Error event to clear the error from the Context selectively (eek).
  3. Disable EventValidation in the web form.
    <%@ Page Language="C#" EnableEventValidation="false" %>
    (gulp)

None of the above are entirely reasonable solutions (especially the last two), and the worst part was that my test page was just a simple contact page that did not change/set roles or cookies, or response.write, or set anything in the session, and wasn't receiving any Unicode character input, or even requiring a user to be logged in, or writing anything to the trace, or anything beyond the basics. And still it blew up. But only occasionally.

In order to faithfully reproduce the error, I finally determined that it must have something to do with sessions as it would only occur if the app pool had recycled and all browser windows had been closed. So, based on one of the comments in one of the above posts, even though I'm not touching session on one of the problem pages, I tried a hack in one of the problem page's Page_Load:

Session["FixAJAXSysBug"] = true;

And lo and behold, we're good to go! So even though I am not using Session on the problem page it must be attempting to set the initial session cookie using the Update Panel callback. So the solution is to make sure the initial session is set before any Update Panel callback takes place. How this got through into production is beyond me.

So if you're sporadically encountering the Sys.Webforms.PageRequestManagerServerErrorException, it could be for any of the above reasons or the fact that your dog/cat/stuffed teddy bear is sitting too close to your monitor. But give the last one a try in every page utilizing AJAX if you're using sessions in your application.

UPDATE: If the problem pages aren't even using session, just turn session off for the page:

<%@ Page EnableSessionState="false" ... %>

Or better yet, set it in your base class to always be off, and turn it on for the pages where you need it on.

UPDATE II: Further developments.

Comments

7/21/2007 12:10:00 PM #

Anonymous

Thank you! This one was starting to drive me crazy. I was setting a session object on a button press which would produce the error, press it again and it worked fine.
I added a session call to page load and now all is rosy.

Anonymous | Reply

10/25/2007 9:37:00 AM #

Elmar

This is unbelievable. I kept restructuring my hole application which is (unfortunatly) rather big and contains lots of dynamically loaded components etc.etc.. I still can´t believe that this did the trick. I searched the net for days but all I found was - do not use response.write... which I did not.

Thanks a lot for posting this!

Elmar | Reply

3/12/2008 1:46:00 PM #

Siderite

Thank you for linking my blog entry, but to be safe, you could also link to the one that describes your problem Smile

siderite.blogspot.com/.../...tmanagerparserer.html

Siderite | Reply

3/22/2008 3:06:00 PM #

Anonymous

Thank you very much for posting this!

I have spend over two days to figure out what the problem was, till i finally (and luckily) found your posting.

Jerry

Anonymous | Reply

4/20/2008 12:57:00 AM #

Zubair.NET!

Voila! It solves the issue Smile

Thanks for sharing.

Zubair.NET! | Reply

8/27/2008 9:28:00 AM #

Yogendra

Hi,

This will not work if we need to store values in Session.

Any advice on this ?

Yogendra | Reply

12/9/2008 12:20:00 PM #

Gagan

Thanks a Lot!!!!!!!!!!!!!!
This problem was driving me crazy from last one month.. I just disabled EventValidation for the page earlier earlier. But now i changed to your solution of setting Session variable in page load. And its working beautifully...
Again thanks a lot...

Gagan | Reply

12/23/2008 6:49:00 AM #

Max

Not neccessary all those reasons. I get this message trying to load a user control programmatically into AJAX tab control and firing control's event because a control is loaded without events.

Max | Reply

2/18/2010 11:56:34 PM #

Manish

Hi,

I have some different scenario. The problem is, I have ajax worked for my end and some other machine I tested. but at the customer end, it gives this error. Does this solutions apply to such scenario or I might have another issue with browser or proxy?

Is there anything I could do using which I can get more information from customer side on this issue.

Manish India | Reply

4/2/2010 11:22:30 AM #

Ted Jardine

Manish,

If the session cookie is already there, it'll be fine. The problem is that sometimes the session cookie is not already there, and the ajax callback attempts to set it which (in previous versions of ASP.NET) is set at the wrong time in the callback lifecycle. That's why it's a sporadic error.

Having said all that, just set your session cookie on initial page load as described and see if it solves your client's issue. If it doesn't, then it's something else and you would need to provide more info.

Ted Jardine Canada | Reply

Add comment


(Will not be posted. Rather, will be used to show your Gravatar icon.)

  Country flag

biuquote
  • Comment
  • Preview
Loading