[转]Sys.WebForms.PageRequestManagerParserErrorException @ MS Ajax
[原文]Sys.WebForms.PageRequestManagerParserErrorException @ MS Ajax
Spending a weeks to troubleshoot the "Sys.WebForms.PageRequestManagerParserErrorException" error in my company’s project.
Finally I found the workaround for it. Very easy, just disable the enableEventValidation on the ASPX page containing the UpdatePanel.
If this workaround doesn’t work for you, you should probably look @ Eilon Lipton’s Blog. Here is some of his findings:
Why do I keeping getting a PageRequestManagerParserErrorException?
Well, chances are you’re doing one of the things mentioned in the error message. Here are the most common reasons and why they don’t work:
-
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. -
Response filters:
Similar to Response.Write(), response filters can change the rendering in such a way that the UpdatePanel won’t know. -
HttpModules:
Again, the same deal as Response.Write() and response filters. -
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. -
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.
How do I avoid getting a PageRequestManagerParserErrorException?
To start with, don’t do anything from the preceding list! Here’s a matching list of how to avoid a given error (when possible):
-
Calls to Response.Write():
Place an <asp:Label> or similar control on your page and set its Text property. The added benefit is that your pages will be valid HTML. When using Response.Write() you typically end up with pages that contain invalid markup. -
Response filters:
The fix might just be to not use the filter. They’re not used very often anyway. If possible, filter things at the control level and not at the response level. -
HttpModules:
Same as response filters. -
Server trace is enabled:
Use some other form of tracing, such as writing to a log file, the Windows event log, or a custom mechanism. -
Calls to Server.Transfer():
I’m not really sure why people use Server.Transfer() at all. Perhaps it’s a legacy thing from Classic ASP. I’d suggest using Response.Redirect() with query string parameters or cross-page posting.
Another way to avoid the parse error is to do a regular postback instead of an asynchronous postback. For example, if you have a button that absolutely must do a Server.Transfer(), make it do regular postbacks. There are a number of ways of doing this:
-
The easiest is to simply place the button outside of any UpdatePanels. Unfortunately the layout of your page might not allow for this.
-
Add a PostBackTrigger to your UpdatePanel that points at the button. This works great if the button is declared statically through markup on the page.
-
Call ScriptManager.RegisterPostBackControl() and pass in the button in question. This is the best solution for controls that are added dynamically, such as those inside a repeating template.