0

I have a <asp:Button /> which is used to export some a DataTable into Excel. I need to display a progress image while the DataTable is being generated. Below is my try for this but still stuck. May be I haven't understand the life cycle here. Appreciate any help.
ASPX

<asp:Button ID="ExportResults" runat="server" UseSubmitBehavior="false" Text="Export Selected" OnClientClick="showWaitingForExport();" OnClick="ExportResults_Click"/>

JavaScript

function showWaitingForExport() {
  $("#progressbar").progressbar({ value: false });}

Code Behind

protected void ExportResults_Click(object sender, EventArgs e)
{
  DataTable resultDT = GenerateDataTable(); //This is the time taking function and after this I need to hide my progressbar while response still not get end

  ScriptManager.RegisterStartupScript(this, this.GetType(), "stopprogress", "$('#progressbar').progressbar('destroy');", true);

        string filename = "Search_Results.xlsx";
        workbook.AddWorksheet(resultDT, "Search Results");
        workbook.SaveAs(Server.MapPath("~/Temp/" + filename));
        Response.ContentType = "application/vnd.ms-excel";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
        Response.TransmitFile(Server.MapPath("~/Temp/" + filename));
        Response.End();
}
highfive
  • 628
  • 3
  • 12
  • 31
  • It is a horrible idea to use Office Interop from ASP.NET or another server technology. These APIs were written for use in a desktop application, for automating Office (a suite of desktop applications). Server applications are different in many ways that make it a very, very bad idea to use Office Interop in them. It's also unsupported by Microsoft, and may violate your Office license. See [Considerations for server-side Automation of Office](http://support.microsoft.com/kb/257757) – John Saunders Jan 22 '14 at 04:14
  • Besides, I don't see you changing any client-side components. – John Saunders Jan 22 '14 at 04:15
  • I think he/she is talking about the visibility of the progress bar when mentioning the "client-side component". To do this I think you would have to use an asynchronous call. Does that sound right? – macoms01 Jan 22 '14 at 04:22
  • Thanks for the quick response and guidance @JohnSaunders Agreeing with you I may be doing this is in wrong way because I'm somewhat new to ASP.NET. For your question I found that by using `SriptManager` I can call client script from code behind, which I'm doing above. Is it possible to change state before the response? – highfive Jan 22 '14 at 04:27
  • 1
    You're also not "calling" any client-side script. You need to learn the basics of web development. The client and server are largely disconnected. The client makes a request - the server sends a response - that's about that. They are usually not both operating at the same time. – John Saunders Jan 22 '14 at 04:34

1 Answers1

0

Unless I misunderstood the question, check out this link - it's a fairly common thing that developers need to do: http://www.dotnetcurry.com/showarticle.aspx?ID=227

OnClientClick is called when you click the button, but then the request goes to the server (OnClick="ExportResults_Click") and the browser/client will show the normal loading page (usually a blank white page). You should be able to use an UpdatePanel with one of the strategies described in the link to show an update panel progress bar. The only way around this (that I'm aware of) is to use an async postback so the page is still displayed while the server is processing.

Does this make sense? Does the article help? I can go into more detail or show an example if necessary. I am not at a computer with Visual Studio to throw together an example, but I can post something tomorrow if no one else has answered.

macoms01
  • 1,110
  • 13
  • 22
  • Thanks @macoms01. This is exactly what I got to know after struggling with internet. But the problem is for security concerns, Export into Excel like functionalities are not working through AJAX requests. `UpdatePanel` also doing same isn't it? – highfive Jan 22 '14 at 04:33
  • So just to make sure, are you worried about the security of exporting files to excel, having a progress bar show, or having issues with the actual file being exported? – macoms01 Jan 22 '14 at 13:28
  • I mean that server data Exporting functionalities normally has been disabled via AJAX reuquests. So using an `UpadatePanel` is not a solution as I think. – highfive Jan 24 '14 at 06:45