当前位置:   article > 正文

Sys.WebForms.PageRequestManagerParserErrorException_parsererror: invalid character in string. if you a

parsererror: invalid character in string. if you are trying to use unicode c

Sys.WebForms.PageRequestManagerParserErrorException

 

我只是用了Ajax很常用的功能,就是将一控件放进UpdatePannel,让它局部更新。很容易实现。可是当我测试的时候,老出现上面的问题。具体问题不说了哈,因为只是一个不熟悉这个平台导致的错误。

我的程序里使用了按钮的PostBackUrl属性。查了,原理就是:它没经过微软的底层对象类的处理。发到导致请求页面文件不能被解析。所以会出现上面的逻辑。当然还有其他几种情况。实质都是一样,反正不要使PostBack相关的属性。时间很仓促,写在这儿了,会改进这文章。

 

 

 

 

下面这篇文章讲得很好,如果你的英语很好,可以去看一下。

Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it

If you've used the Microsoft ASP.NET AJAX UpdatePanel control, there's a good chance you've hit the "Sys.WebForms.PageRequestManagerParserErrorException" error.

What's a PageRequestManagerParserErrorException?

The UpdatePanel control uses asynchronous postbacks to control which parts of the page get rendered. It does this using a whole bunch of JavaScript on the client and a whole bunch of C# on the server. Asynchronous postbacks are exactly the same as regular postbacks except for one important thing: the rendering. Asynchronous postbacks go through the same life cycles events as regular pages (this is a question I get asked often). Only at the render phase do things get different. We capture the rendering of only the UpdatePanels that we care about and send it down to the client using a special format. In addition, we send out some other pieces of information, such as the page title, hidden form values, the form action URL, and lists of scripts.

As I mentioned, this 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, the format will be messed up. Perhaps the most common way to do this is to call Response.Write() during Page's Load event, which is something that page developers often do for debugging purposes.

The client ends up receiving a blob of data that it can't parse, so it gives up and shows you a PageRequestManagerParserErrorException. Here's an example of what the message contains:

---------------------------
Microsoft Internet Explorer
---------------------------
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 'Hello, World!106|upd'.
---------------------------
OK  
---------------------------

If you ask me, this error message is not all that bad. After all, I'm the one that made it :) The details indicate what was being parsed when it decided to give up. You can see the part of the text from my Response.Write(), and immediately after that is part of the special format I keep mentioning.

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:

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.

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):

1.      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.

2.      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.

3.      HttpModules:
Same as response filters.

4.      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.

5.      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:

1.      The easiest is to simply place the button outside of any UpdatePanels. Unfortunately the layout of your page might not allow for this.

2.      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.

3.      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.

Summary

I hope I've answered a lot of questions here and not angered too many of you. We're looking at ways to improve some of these situations in the next version of ASP.NET, but of course there are no guarantees. If you avoid changing the response stream, you're good to go. If you absolutely must change the response stream, simply don't do asynchronous postbacks.

 

Published Monday, February 26, 2007 6:00 PM by Eilon

Filed under: Atlas, ASP.NET, AJAX, UpdatePanel

Comments

# re: Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it

Tuesday, February 27, 2007 4:27 AM by Atit

Can u get me..information on known AJAX ASP.net RC1 bug.

Description.

 For IE7, click on any asp:imagebutton on  extreme top or left corners that initates postback.

You will get System.Format exception becuase IE7 passes ImageClickEventArgs e  values wrong

e.X=NaN

and e.Y=-1

so e.x creates parsing exception.

Note:It works perfectly in firefox.It occurs in IE7 and IE6.

Atit.

# re: Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it

Tuesday, February 27, 2007 4:51 AM by Mike

You forgot the bug in the role provider. It's on the asp.net forums, some people have to disable caching roles in cookies to avoid the error. It seems that the role provider adds the cookie to the response when it's too late.

# re: Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it

Tuesday, February 27, 2007 8:58 AM by Dominick

I'm getting that error right now. I have an update panel with a wizard control inside. On a step I have a button that changes the text of a textbox. Before I click the button I can navigate to any step just fine, but I get that error on the first step navigation click after I click the button that adds the text. It doesn't happen every time though. Very strange. The message says the error occurs at:

'able>

| <HTML>

<Head>

# re: Sys.WebForms.PageRequestManagerParserErrorException

 

 

 

既然与长时间不操作有关,可能,你用了Session,而Session在服务器端丢失。
解决:1、用数据库存Session试试。
      2、用客户端脚本,判断错误类型,如果出现这种错误,自动reload
   使用ScriptManagerEndRequestHandler事件。将以下Javascript加入到 <ScriptManager> 标签后面, 
 
注意不能放在 <Head> 中。 
  程序代码 <script   language= "javascript ">   Sys.WebForms.PageRequestManager.getInstance().add_endRequest 
 
(EndRequestHandler);   
function   EndRequestHandler(sender,   args)   
{   
if   (args.get_error()   !=   undefined)   
{   
if(args.get_error().message.substring(0,   51)   ==   "Sys.WebForms.PageRequestManagerParserErrorException ")   
{ 
window.location.reload();   //出现Session丢失时的错误处理,可以自己定义。 
}   
else 
{ 
alert( "发生错误!原因可能是数据不完整,或网络延迟。 ");   //其他错误的处理。 
} 
args.set_errorHandled(true);   
} 
}   
</script>
 
但愿能解决你的问题,现金就不必了,可见你的诚恳。
************************************
if(args.get_error()。。。。语句位置,加上alert(args.get_error().message);

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/108740
推荐阅读
相关标签
  

闽ICP备14008679号