I have a gridview call gvfamilies on a page with a button called button1. I populate the gridview and I want to export the contents of the gridview to an excel file called test.xls
I have searched the internet and have found several examples of code but I can't seem to get any to work. Can you tell me what is wrong with the following code. I get an error message of
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 '<div>
Here is my code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Call ExportToExcel("test.xls", gvFamilies)
Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
Response.AddHeader("content-disposition", "attachment;filename=Demand_Report_" & Date.Today.ToString("MM-dd-yyyy") & ".xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
Dim StringWriter As New System.IO.StringWriter()
Dim HtmlTextWriter As New HtmlTextWriter(StringWriter)
Response.Write(StringWriter.ToString()) 'This is where it errors
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)