0 comments
Wow! Finally! By chance I stumbled across another blog post on the web, where a solution (or temporary fix until something better turns up) to my ongoing PageRequestManagerParserErrorException problems.

Basically the problem is that on a page where I use the Microsoft AJAX.NET framework, I get intermittent popups when viewing the pages in Firefox. The popups contain the nice, short, concise error message "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. Details: Error parsing near ''"

Now I finally found someone who came up with a simple yet effective hack to mask/solve the problem. The solution involves adding some Javascript to your pages and then overriding the OnInitCompleted method on the server side.
Here's the JavaScript code:

<script type="text/javascript" language="javascript">
window.isFireFox = (navigator.userAgent.indexOf("Firefox") != -1);


if(window.isFireFox)
{
        window.addEventListener('load',OnPageLoad,false);
        function OnPageLoad()
        {
                 Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(OnBeginRequest);       
        }

        function OnBeginRequest(sender, args)
        {
                var delta = args.get_request().get_headers()['X-MicrosoftAjax'];    
                if(delta == 'Delta=true')    
                {
                    var body = args.get_request().get_body() + 'X-MicrosoftAjax=' + encodeURIComponent('Delta=true');        
               args.get_request().set_body(body);    
                }
         }
}
</script>


 

And here's what goes in your codebehind:


 
protected override void OnInitComplete(EventArgs e)
{
  #region Firefox Fix

  ScriptManager sm = ScriptManager.GetCurrent(this);
  if (IsPostBack && Request.Browser.Browser == "Firefox" &&
   sm != null &&
   sm.IsInAsyncPostBack == false &&
   Request.Form["X-MicrosoftAjax"] == "Delta=true")
  {
    sm.GetType().GetField("_isInAsyncPostBack",
      System.Reflection.BindingFlags.NonPublic |
      System.Reflection.BindingFlags.Instance).SetValue(sm, true);
  }
  #endregion
  base.OnInitComplete(e);
}


Lo, and behold, it seems to work! Thanks to M. Bhaskar for the solution!

comments powered by Disqus