Response.End () throws error while exporting gridview to excel file

I have this code:

protected void ibtGenerateReport_Click(object sender, ImageClickEventArgs e) { string filename = "report.xls"; StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter); DataGrid DataGrd = new DataGrid(); DataGrd.DataSource = odsLSRAudit; DataGrd.DataBind(); DataGrd.RenderControl(htmlWrite); System.IO.StreamWriter vw = new System.IO.StreamWriter(filename, true); stringWriter.ToString().Normalize(); vw.Write(stringWriter.ToString()); vw.Flush(); vw.Close(); WriteAttachment(filename, "application/vnd.ms-excel", stringWriter.ToString()); } public static void WriteAttachment(string FileName, string FileType, string content) { HttpResponse Response = System.Web.HttpContext.Current.Response; Response.ClearHeaders(); Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName); Response.ContentType = FileType; Response.Write(content); Response.End(); } 

But Response.End() gives me the following error:

Microsoft JScript runtime error: 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 '<table cellspacing="'.

Response.Write(content) has the correct information, as I see it. But the Save / Open dialog box does not appear, in addition to the above error.

I am using UpdatePanels.

+6
source share
5 answers

Add this to the Page_Load () method.

Assuming ibtGenerateReport is your button

 protected void Page_Load(object sender, EventArgs e) { ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(this.ibtGenerateReport); //Further code goes here.... } 

Explanation:

The UpdatePanel control uses asynchronous callbacks to control which parts of the page will be displayed. He does this using a whole bunch of JavaScript on the client and a whole bunch of C # on the server. This is output using a special format that JavaScript can understand on the client. If you ruin the format by rendering things outside the page rendering phase, the format will be ruined.

Why do I keep getting a PageRequestManagerParserErrorException?

Well, most likely you are doing one of the things mentioned in the error message. Here are the most common reasons and why they do not work:

Calling Response.Write (): By calling the Response.Write () function directly, you bypass the regular ASP.NET control rendering mechanism. The bits you write go directly to the client without further processing.

Response Filters / HttpModules: Like Response.Write (), they can change the rendering in such a way that the UpdatePanel will not know.

Server tracing is enabled: Tracing is efficiently written using Response.Write (), and as such will ruin the special format that we use for UpdatePanel.

Calls to Server.Transfer (): Unfortunately, there is no way to detect that Server.Transfer () has been called. This means that UpdatePanel cannot do anything smart when someone calls Server.Transfer (). The response sent to the client is the HTML markup from the page you went to. Since its HTML, and not a special format, it cannot be parsed and you will get an error.

Solution: One way to avoid a parsing error is to make a regular postback instead of asynchronous postback by calling ScriptManager.RegisterPostBackControl ()

Please refer to the full Explained and other solutions from the Ellon Lipton blog here.

+24
source

Change Response.End with this:

 Response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); 

Response.End is bad practice, it interrupts the thread to bypass the rest of the operations in the life cycle of the HTTP request and throws an exception.

+4
source

Have you tried Response.Clear () before writing anything in the response, and then Response.Flush () until the end of the response?

Also, it never hurts to pass true to Response.End () to avoid HTTP exception errors when redirecting. Just make sure that the rest of the code does not perform additional processing that you do not want.

0
source

Since you are trying to do this with UpdatePanels, you should try using jQuery Ajax and write this code on a separate page in the page load event, in order to get the file when the request to this page is called. The new answer actually removes the information needed for analysis in response to a previous update panel request.

0
source

This code conflicts with some UpdatePanel / AJAX in your aspx.

Try writing this to aspx:

Code - that should work !: ')

0
source

All Articles