Stopbyte

ASP.NET - How to Show/Hide Loading Image after Export to Excel without using Update Panel?

I have googled around for half day searching for the answer but no success. I’m trying my luck here.

My ASP.Net does not have update panel.
I succeed to show progress bar after user click Export to Excel but it never hides back after Respond.End.
Is there any method to solve the problem?

Here’s the codes:

Javascript to show progress bar:

<script type="text/javascript">
   function ShowProgress() {
       setTimeout(function () {
           var modal = $('<div />');
           modal.addClass("modal");
           $('body').append(modal);
           var loading = $(".loading");
           loading.show();
           var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
           var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
           loading.css({ top: top, left: left });
       }, 200);
   }
   $('form').live("submit", function () {
       ShowProgress();
   });
</script>

Code behind (Page Load) :

ClientScript.RegisterStartupScript(Me.[GetType](), "load", "$(document).ready(function () { $('[id*=btnExport]').click(); });", True)

Code behind (Export to Excel) :

Dim style As String = "<style> .text { mso-number-format:'#,##0.000_);[Red](#,##0.000)';text-align:right; } </style> "
 
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fName))
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble())
HttpContext.Current.Response.Write(style)
 
Using sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
 
    'To Export all pages
 
    For Each row As GridViewRow In gv.Rows
        'For Each cell As TableCell In row.Cells
        For i As Integer = 0 To row.Cells.Count - 1
            Dim str As String = row.Cells(i).Text
            If i > 7 Then
                row.Cells(i).Attributes.Add("class", "text")
            End If
        Next
    Next
 
    pnl.RenderControl(hw)
 
    HttpContext.Current.Response.Write(sw.ToString())
 
End Using
 
 
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
Response.[End]()
1 Like

I don’t know what would be the reason behind that behavior it’s really your own case dependent. but here are some few tips that might help:

  1. First should know the difference between Response.Flush & Response.End:
    Response.Flush
    Forces all currently buffered output to be sent to the client. The Flush method can be called multiple times during request processing.
    .
    Response.End
    Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event.
     
    So maybe calling Response.End is not needed and mostly it’s what can cause your problems.

  2. For the Code behind i guess you should change it so that instead of clicking the button, you hide the loading image instead.

  3. Or a better alternative would be using Javascript this way:

 

Sys.Application.add_init(my_init);
 
function my_init() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();               
    prm.add_endRequest(hideMethod);
}
 
function hideMethod() {           
    $('#YourLoadingImage').hide();
}