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.

1
  • Did you look at the markup after post back of your button click to see if the javascript was written to your page? Commented Oct 6, 2015 at 12:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Athul Refer this link - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.