I built a similar inherited GridView control with the ability to export CSV. The way I handled this is to make the export button a simple <a href> current page with the special parameter querystring "?SortablePagableGridViewExportToCSV=XXX" , where XXX is the UniqueID my control. I override the Render method of my control, and there I check the request parameters manually to see if this export parameter has been sent. If so, I write the header of the CSV content type and the response as shown below.
Me.Page.Response.Clear() Me.Page.Response.ClearContent() Me.Page.Response.ClearHeaders() Me.Page.Response.ContentType = "text/csv" Me.Page.Response.AddHeader("Content-Disposition", "attachment;filename=" & Me.ExportToCsvFileName) Me.Page.Response.End()
The key is calling Page.Response.End() , which stops further processing. This is a GET request, so I don’t have to worry about the back of UpdatePanel asynch or something like that. The browser behaves fine in this situation, pulling out a download link, but keeping the user on the current page. The browser URL does not change, and the user can even right-click the download link and use Save Target As ... if they wish.
source share