1

Okay, I've seen this asked in a couple of spots but never really got an answer that helped or that I understood. I'm just starting back on ASP.net and am using VS 2010 on the 4.0 framework.

I found some code online to allow you to generate an .xls Excel file from a dataset.

The code is below, but when I run this code from a button outside of my ajax update panel it works perfectly. If I execute the code from a button inside the update panel (where I need it to be) then I get the following:

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

I have tried the two versions of the header shown and have tried the completerequest() instead of response.end.

Could someone explain to me why this doesn't work in the update panel and how I could make it work in the update panel?

Thanks in advance!

Protected Sub ExportDataSetToExcel(ByVal ds As DataSet, ByVal filename As String)
    Dim response As HttpResponse = HttpContext.Current.Response

    ' first let's clean up the response.object
    response.Clear()
    response.Charset = ""

    ' set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel"
    'response.ContentType = "application/octet-stream"
    response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")

    ' create a string writer
    Using sw As New StringWriter()
        Using htw As New HtmlTextWriter(sw)
            ' instantiate a datagrid
            Dim dg As New DataGrid()
            dg.DataSource = ds.Tables(0)
            dg.DataBind()
            dg.RenderControl(htw)
            response.Write(sw.ToString())
            'HttpContext.Current.ApplicationInstance.CompleteRequest()
            response.End()
        End Using
    End Using
End Sub
timber
  • 21
  • 2
  • 3
  • You should move this feature from Server-side Page code to a separate Generic Handler (ashx). With this option, It is assumed that the data can be accessed on the server side. Let me know if you're interested in more info. Affecting the response in the code-behind is bad news if you are using AJAX controls. – Glenn Ferrie Jul 14 '11 at 02:01
  • Reading up on those now, Yeah I would love more information. Is it possible to pass a dataset/datatable to the generic handler as well? – timber Jul 14 '11 at 02:30
  • Well... yeah if you serialize, compress and convert from byte[] to base64 string and send it in the querystring, but isn't the data available on the server side? As an alternative, change your code to save the "export file" to disk, then use the ashx to grab it from the disk, download it and then delete it from the temp location on the server – Glenn Ferrie Jul 14 '11 at 02:33
  • I've posted a similar sample on a prev question... I will post a link – Glenn Ferrie Jul 14 '11 at 02:33
  • This worked for me. http://stackoverflow.com/questions/11221033/sys-webforms-pagerequestmanagerparsererrorexception-the-message-received-from-t – Agnes Simpson Nov 26 '15 at 08:13

3 Answers3

1

Try this:

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

Do not call reposne.End()

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • Okay, I modified it as you said above and I still get the same error when trying to run it from inside the update panel... – timber Jul 14 '11 at 02:27
  • Does your `aspx` have any markup in it? It probably needs to be empty. Also noticed `response.Charset`. Have you tried setting it to something proper (like `UTF-8`) – Mrchief Jul 14 '11 at 15:13
0

Sorry, I know is a bit late, but found the answer right now.

Try with this answer. I resolve it doing what it is said here and it works perfectly

Community
  • 1
  • 1
Claudia
  • 576
  • 2
  • 7
0

check out this sample:

How to download/open the file that I retrive by server path?

it shows how to write a handler to DL a file.

Community
  • 1
  • 1
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73