0

i have a button click event

protected void btndwnReport_Click(object sender, EventArgs e)
    {
        try
        {                
            save("Report1"); 
        }
        catch (Exception ex)
        {
            Log.Errlog("Error Occured in  btndwnReport_Clickof UI_Report Page : " + ex.Message.ToString());
        }
        finally
        {                
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "HideLoading", "HideLoading2();", true);//this function is not triggering
        }
    }

using this i am trying to download a excel file using reportviewer. On clicking this button i am showing one loading icon(by calling ShowLoading2()) which is defined as a jquery function.

function ShowLoading2() {
            try {    
                if (parent.document.getElementById('dvProgress'))
                    $("#dvProgress", parent.document).hide();
            } catch (e) { }

            $("#dvProgress").show();            

        }
function HideLoading2() {
            try {
                if (parent.document.getElementById('dvProgress'))
                    $("#dvProgress", parent.document).hide();
            } catch (e) { }
            $("#dvProgress").show();
            $("#dvProgress").fadeOut(15000);

        } 

I am able to download the report in excel format but not able to call HideLoading2() function from code behind after downloading the excel.

When save("Report1"); method is commented ,able to call HideLoading2().

Here is the save method

public void save(string ReportName)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType, encoding, extension, deviceInfo;
        string format = "Excel"; byte[] bytes = null;
        deviceInfo = "True";       

        bytes = rptViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamids, out warnings);

        Response.Buffer = false; //transmitfile self buffers
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "GetReport/excel";            
        Response.AddHeader("Content-Disposition", "attachment; filename=" + ReportName + ".xls");
        Response.OutputStream.Write(bytes, 0, bytes.Length);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();
        Response.Flush();
        Response.Close();
    }

How can i call the HideLoading2() function after downloading the excel sheet?

Note: I am not using scriptmanager\updatepanel in the page.

AcAnanth
  • 765
  • 3
  • 19
  • 53
  • Did you look at the markup after post back of your button click to see if the javascript was written to your page? – Adam Heeg Oct 06 '15 at 12:37

1 Answers1

1

Once you call Response.Flush() and Response.Close() which is required in case of file download, server stops the processing and return the response. After that you don't have an option to execute code.

Generally for downloading files, try calling the function via JavaScript asynchronously and use window.open for opening the file.

Cheezy Code
  • 1,685
  • 1
  • 14
  • 18
  • 1
    @Athul Refer this link - http://stackoverflow.com/a/15961851/5188835 You can add a query string and check it in Page_Load, if it exists call the required function at server end and return the response. – Cheezy Code Oct 06 '15 at 12:52