Sys.webforms.PageRequestManagerParserErrorExcept(ajax刷新用response或transfer跳转遇到的问题解决办法)
来源:互联网 发布:大华股份 人工智能 编辑:程序博客网 时间:2024/05/18 17:39
最近在用AJAX时遇到这样一个问题"Sys.webforms.PageRequestManagerParserErrorException".
最终查到一种解决方法如下:
Ajax是无刷新的,而使用Response进行带参重定向时需要刷新页面。所以只须在UpdatePanel下设置“asp:PostBackTrigger”的“ControlID”为指定的控件名称即可,如:
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
但其中的"btnSave"是什么呢?我的看是要看情况而定不一定是"button"
我的做法如下:
<body style="margin-bottom:0; margin-left:0; margin-right:0; margin-top:0">
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" BackColor="Red" ForeColor="Red" Height="400px"
Width="300px">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID ="Datalist1" />
</Triggers>
<ContentTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ForeColor="Blue" Height="30px"
Width="100px" /><br />
<asp:DataList ID="DataList1" runat="server" DataKeyField="userid" RepeatColumns="1" OnEditCommand="DataList1_EditCommand" OnItemCommand="DataList1_ItemCommand" >
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="30px" BackColor="Black" ImageUrl='<%# Eval("userimage") %>'
Width="30px" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="go"><%# DataBinder.Eval(Container.DataItem,"username") %></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
<asp:Timer ID="Timer1" runat="server" Interval="600" OnTick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</asp:Panel>
</div>
</form>
</body>
参考的英文文档:
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:
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.
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.
- Sys.webforms.PageRequestManagerParserErrorExcept(ajax刷新用response或transfer跳转遇到的问题解决办法)
- AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序
- AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序
- AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序
- AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序
- AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序
- AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序
- Ajax学习笔记(4)Sys.WebForms与服务端协同工作以及Sys.Application下的属性
- Sys.WebForms.PageRequestManagerParserErrorException的解决方法
- Sys.WebForms.PageRequestManagerParserErrorException的解决方法
- 使用Ajax控件错误:sys.WebForms.PageRequestManagerServerErrorException
- avoid Sys.WebForms.PageRequestManagerTimeOutException while using Ajax
- Ajax 与 Sys.WebForms.PageRequestManagerParserErrorException异常
- Eclipse遇到的问题解决办法
- 网页跳转:Server的Transfer和Response的Redirect方法(总结)
- asp跳转页面Server的Transfer和Response的Redirect
- 页面跳转 Server.Transfer和 Response.Redirect的区别
- 新瓶旧酒ASP.NET AJAX(6) - 客户端脚本编程(Sys.WebForms命名空间下的类Sys.Serialization命名空间下的类)
- 书写更利于思考
- 给同事建议,如何能够真正学习成为Oracle高手?
- 数据源控件如何使用Session做参数,尤其是参数是一个对象时候
- 第一篇博文
- 新学期听课有感
- Sys.webforms.PageRequestManagerParserErrorExcept(ajax刷新用response或transfer跳转遇到的问题解决办法)
- 一个老油条面试的忠告
- 用jar 命令打包war包
- 制作ghostxp 系统盘
- 新学期的每一天
- 归并排序算法MergeSort in C#
- Oracle学习笔记[4] - ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- 冒泡排序源代码
- Servlet中文乱码解决方案