Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed

Tags:

c#

asp.net

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 btnExcelExport is the button which will export the Grid Content in to Excel Sheet :-

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:

"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed."

like image 854
Dharmendra Kumar Singh Avatar asked Jun 27 '12 07:06

Dharmendra Kumar Singh


2 Answers

I fixed this issue. As I'm using UpdatePanel, I added below code in the Page_Load event of the page and it worked for me:

protected void Page_Load(object sender, EventArgs e) {   ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);   scriptManager.RegisterPostBackControl(this.btnExcelExport);   //Further code goes here.... } 
like image 192
Dharmendra Kumar Singh Avatar answered Oct 01 '22 04:10

Dharmendra Kumar Singh


In my case, the problem was caused by some Response.Write commands at Master Page of the website (code behind). They were there only for debugging purposes (that's not the best way, I know)...

like image 30
Marcelo Luz Avatar answered Oct 01 '22 04:10

Marcelo Luz