Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
12004
WebDataGrid Excel Asynchronous Export Example in ASP.NET AJAX
posted

Attached is an example for exporting an excel file asynchronously from the WebDataGrid and downloading the export file by a single button click. I have found that there doesn’t seem to be a straightforward way for doing this so there is some complexity added. The main reason is because in ASP.NET AJAX does not support Response.Write() or Response.BinaryWrite() methods so I wrote some code for handling an event for “add_endRequest” of the PageRequestManager. If you curious about PageRequestManager well it is the object that will allow you to handle client side events for ASP.NET AJAX in response from async postbacks.

In the example first loads the data into the grids. After clicking on the export button will create an instance of the Workbook object and I just stored it in a Session object. Since the button is inside the UpdatePanel will trigger an asynchronous postback upon the button click and will call the event handler “end_request()”. The event handler just happens to set a new query string name “action=DownloadFile” that will execute upon page load. When page load is executed and Session object is populated then the Response will be added to the Header as an attachment of “ExportedTo.xls”. After adding the header will call BinaryWrite() so the user can download the file.

In the “Button1_Click” event handler I added Thread.Sleep(3000) so that the program will wait 3 seconds before exiting the method. That way you can see the animation of the “ajax-loader.gif” file to shown when the ProgressTemplate is shown.

WebDataGrid_Async_Export_CS.zip