I have a button on my page that allows a user to save the contents of a GridView
to Excel. The class that does the work is derived from a View
and not a Page
here is the code so far:
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=file.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringBuilder sb = newStringBuilder();
StringWriter sw = newStringWriter(sb);
Html32TextWriter htw = newHtml32TextWriter(sw);
Page page = new Page();
HtmlForm f = newHtmlForm();
page.Controls.Add(f);
f.Controls.Add(gridView);
HttpContext.Current.Server.Execute(page, htw, true);
Response.Write(sb);
This results in a file being created and the user can download it but when opened in Excel the entire contents of the gridview's containing form is shown (including any JS):
Because this done in a View
and not a Page
I cannot use:
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
...and when using:
gridView.RenderControl(...);
I get a 404 error when clicking on the button.
How do I target just the gridview and allow that to be added to the Response
in a suitable format?