C# Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11221033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed
提问by Dharmendra Kumar Singh
I have a grid view on my page and I want to export it to the Excel Sheet,
Below is the code I had written to do this task, here I am already passing the dataset to the method to bind the grid and btnExcelExportis the button which will export the Grid Content in to Excel Sheet :-
我的页面上有一个网格视图,我想将其导出到 Excel 工作表,下面是我为完成此任务而编写的代码,在这里我已经将数据集传递给绑定网格的方法,并且btnExcelExport是按钮将网格内容导出到 Excel 工作表中:-
private void BindGridView(DataSet ds)
{
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
GVUserReport.DataSource = ds;
GVUserReport.DataBind();
btnExcelExport.Visible = true;
}
}
}
protected void btnExcelExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition","attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GVUserReport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
Now when I am debugging I found that the grid is binded sucessfully but when trying to export it to Excel, I'm getting this error:
现在,当我调试时,我发现网格已成功绑定,但是在尝试将其导出到 Excel 时,出现此错误:
"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed."
“Microsoft JScript 运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。”
采纳答案by Dharmendra Kumar Singh
I fixed this issue. As I'm using UpdatePanel, I added below code in the Page_Loadevent of the page and it worked for me:
我修复了这个问题。当我使用时UpdatePanel,我Page_Load在页面事件中添加了以下代码,它对我有用:
protected void Page_Load(object sender, EventArgs e) {
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExcelExport);
//Further code goes here....
}
回答by Krishna Tiwari
Add this to you PageLoad and it will solve your problem:
将此添加到您的 PageLoad,它将解决您的问题:
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.lblbtndoc1);
回答by Marcelo Luz
In my case, the problem was caused by some Response.Writecommands at Master Pageof the website (code behind). They were there only for debugging purposes (that's not the best way, I know)...
就我而言,问题是由网站母版页上的一些Response.Write命令引起的(代码隐藏)。他们只是为了调试目的(这不是最好的方法,我知道)......
回答by Muhammad Ashhar Hasan
I had the same error, then I tried <asp:PostBackTrigger ControlID="xyz"/>instead of AsyncPostBackTrigger.This worked for me. It is because we don't want a partial postback.
我有同样的错误,然后我尝试<asp:PostBackTrigger ControlID="xyz"/>代替AsyncPostBackTrigger。这对我有用。这是因为我们不想要部分回发。
回答by Nuno Ribeiro
1- Neveruse Response.Write.
1-永远不要使用 Response.Write。
2- I put the code below after create (not in Page_Load) a LinkButton (dynamically) and solved my problem:
2-我在创建(不是在 Page_Load 中)一个 LinkButton(动态)之后将代码放在下面并解决了我的问题:
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(lblbtndoc1);
回答by meJustAndrew
Thisworked for me too, but with an addition (below).
这对我也有用,但有一个补充(如下)。
protected void Page_Load(object sender, EventArgs e) {
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExcelExport);
//Further code goes here....
}
I was registering a script on my button to click on another button after its event was finished processing. In order for it to work, I had to remove the other button from the Update Panel (just in case somebody faces the same problem).
我正在我的按钮上注册一个脚本,以便在其事件处理完成后单击另一个按钮。为了让它工作,我不得不从更新面板中删除另一个按钮(以防万一有人遇到同样的问题)。
回答by Trisped
I added the control to the Triggerstag in the update panel:
我Triggers在更新面板的标签中添加了控件:
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="exportLinkButton" />
</Triggers>
</asp:UpdatePanel>
This way the exportLinkButton will trigger the UpdatePanel to update.
More info here.
这样 exportLinkButton 将触发 UpdatePanel 更新。
更多信息在这里。
回答by rayt
For my VB.Net Friends -
对于我的 VB.Net 朋友 -
Dim scriptManager As ScriptManager = scriptManager.GetCurrent(Me.Page)
scriptManager.RegisterPostBackControl(Me.YourButtonNameHere)
回答by felixy
What worked for me was setting aspnet:MaxHttpCollectionKeys to a high value on appSettings tag on the inetpub VirtualDirectories\443\web.config file:
对我有用的是在 inetpub VirtualDirectories\443\web.config 文件上的 appSettings 标记上将 aspnet:MaxHttpCollectionKeys 设置为高值:
<configuration>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="100000" />
</appSettings>
</configuration>

