Monday, March 26, 2012

Export to excel and UpdatePanel problems

As I have noticed myself and while looking around for a good answer, a normal asp button to export to excel does not work inside UpdatePanels since Response lines are not viable inside UpdatePanels. It throws an exception that states something to same affect, "common causes for this error are when the response is modified", etc.

I have looked around and was not able to find a complete answer to work around this problem. The shortened code that I am using is below:

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
frm.Controls.Add(gv)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.Flush():Response.Close():Response.End()

I guess the question is obvious... Is there a clean way around this besides taking the button out of the UpdatePanel? I say this since it does look silly to have one button hanging out by itself in the corner of the page somewhere... I am still new to this so I would much appreciate it if you could "dumb" down the answers a bit.

A bit off topic but would there be a way to handle this on the master page? I believe I just asked two questions ( 1. Update Panel and export to Excel issues, and 2. How to handle this in a master page) but I am mainly focused on the Export issue.

I am able to provide further information as needed. Thanks in advance to all.

First I believe you don't want to add an UpdatePanel around the response.

However why are you doing this? Response.Flush():Response.Close():Response.End()?

Just use Flush but do not close the Response


I saw both ways on the web in different threads that use both ways, without the close and end and with the close and end. However, having the habit of closing what I open, I naturally went with closing it.

BTW, the close and end did not seem to be the cause of the problem as the export as the same problem persists.


I met this problem too. when I put the export buttom into a updatepannel, it will display some javascript error. I think the most possibility of this problem that is you put the Response.contentType in the code as updatePannel use the XMLHTTP to aysn communicate with the server.

Response.ContentType = "application/vnd.ms-excel"

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")

Big Smile

No comments:

Post a Comment