Excel datagridview export issues

I am trying to export datagrid to excel. For some reason, the known working methods don't work. The export is done from a custom control. My page (default.aspx) is using the master page and the page has a custom control, there is actually a data file that I am trying to export.

Here's my ascx code:

Response.ClearContent();
 Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
 StringWriter sw = new StringWriter();
 HtmlTextWriter htw = new HtmlTextWriter(sw);
_gvwResults.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

      

In my default.aspx (the page containing the ascx) is the code:

public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. - required to export file */
}

      

Here is the error I'm getting: Sys.Webforms.pagerequestmanagerparsererrorexception. The message received from the server could not be parsed. Common causes for this error are responding to changes to Response.Write () calls, response filters, httpmodules, or server trace.

Any ideas? This code should work, but it is almost as if the response object is not being cleared. Ideas?

0


a source to share


1 answer


It turns out that since the combobox was ajaxified, the export didn't happen. The event was firing, but as the error message says, it was a reaction. Writing to an existing page, thus throwing an error that would prevent the new document from being rendered (xls in this case). After you set up the combobox to do the postback on the page and not activate it, the export started working.



0


a source







All Articles