0

I have button on my ASP.NET page which calls gets disabled on click(to prevent double click) and call this function:

Private Sub ExportToExcel(ByVal nameReport As String, ByVal wControl As GridView, ByVal sTitle As String)
    Dim responsePage As HttpResponse = Response
    Dim sw As New StringWriter()
    Dim htw As New HtmlTextWriter(sw)
    Dim pageToRender As New Page()
    Dim form As New HtmlForm()

    wControl.AllowPaging = False
    wControl.EnableViewState = False
    wControl.DataSource = GetDataSource(False)
    wControl.DataBind()

    wControl.Caption = "<strong>" + sTitle + "</strong>"
    form.Controls.Add(wControl)
    pageToRender.Controls.Add(form)
    responsePage.Clear()
    responsePage.Buffer = True
    responsePage.ContentType = "application/vnd.ms-excel"
    responsePage.AddHeader("Content-Disposition", "attachment;filename=" & nameReport)
    responsePage.Charset = "UTF-8"
    responsePage.ContentEncoding = Encoding.Default
    pageToRender.RenderControl(htw)
    responsePage.Write(sw.ToString())
    responsePage.End()
End Sub

Which I use to export some data to Excel. Now I need to re-enable the button on client side when the HttpResponse is finished. How can I do that?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ericpap
  • 2,917
  • 5
  • 33
  • 52
  • Is this an AJAX call? – jwatts1980 Oct 08 '14 at 22:33
  • No i use a complete page refresh (Postback). I could not make it to work with Ajax but that's ok. I can live with that as long as I can enable my button. – ericpap Oct 08 '14 at 22:39
  • Since the server processes the request, the only way it can be done is with client javascript, and I don't know if it's possible to detect since the request is a separate request than the original. You could just use `window.setTimeout(function() { enableButton() }, 750);` – Brian Mains Oct 08 '14 at 22:40
  • If your page isn't posting back under any other conditions/scenario, you could just re-enable the button inside of a `If Page.IsPostBack Then ... End If` inside of your Page_Load handler. Or maybe I am wrong.. – adaam Oct 08 '14 at 22:41
  • @addam, I Can't do that because the button is disabled on client-side with JavaScript. Server know nothing about it – ericpap Oct 08 '14 at 22:44
  • I have done something similar using [John Culviner's File Download](http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/). This uses a cookie as part of the file download to inform the browser that the download is complete. I used with with [BlockUI](http://malsup.com/jquery/block/) which disables the entire UI instead of just one button. You should be able to see how it works and modify it to enable the button. Or just use BlockUI – Gridly Oct 08 '14 at 22:59

1 Answers1

1

The Response.End() aborts the thread and sends the execution to Application_EndRequest. There is not much to do after that. Please check this post. It presents a solution using iframes.

Community
  • 1
  • 1
Turay Melo
  • 114
  • 5