Sys.WebForms.PageRequestManagerParserErrorException:The message received from the server could not be parsed.
Common causes for this error are when the response is modified by calls to response.Write(), response filters, HttpModules, or server trace is enabled.
Well, why do these cause the exception?
The updatepanel uses asynchronous postbacks. Asynchronous postbacks have the same life cycle and go through the same life cycle events as regular postbacks except for the rendering phase. Only the content that goes in the update panel is captured and sent to the client after combining the content with other pieces of information like page title, hidden form values, form action url and lists of scripts. The content is rendered out using a special format that the JavaScript on the client can understand. If you mess with the format by rendering things outside of the render phase of the page, by using Response.Write(), the client ends up receiving a blob of data that it can't parse, so it gives up and shows you a PageRequestManagerParserErrorException.
How to get around this exception and get the results you need?
If you can avoid writing code, that causes this exception, that is the route to take.
But, sometimes for example you may absolutely have to use Response.Write() because you want to open an active report in PDF format on a button click or you want to download a file. The solution then is to do a regular postback instead of an asynchronous postback.
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 or for those control inside a user control.
Here is example code you can add in your page_Load event
ScriptManager sm = (ScriptManager)Page.Master.FindControl("ToolkitScriptManager1");
sm.RegisterPostBackControl(GeneratePDFReportButton);
No comments:
Post a Comment