Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello friends,

I have a task to download a gridview data to excel as it is shown in gridview.
I did this and the problem is that when the download is complete, I need to close the window as it is literally blank.
I can either show a message and button to close the window but anyhow I have to close the window.
Please help. I have code something like this.
C#
if (dt.Rows.Count > 0)
{
        int RowsCount = dt.Rows.Count;
        System.Random rd = new System.Random(DateTime.Now.Millisecond);
        int MyValue = rd.Next(1000000, 99999999);
        sUniqueName = MyValue.ToString();

        // Create a new workbook.
        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
        SpreadsheetGear.IRange cells = workbook.Worksheets[0].Cells;

        #region Formatting Upper Table Datas            

        #endregion
        var timeUnit = dt.Rows[0]["ContractTimeUnit"].ToString();
        var lineItemTotal = dt.Rows[0]["LineItemTotal"].ToString();

        cells.CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.None);
        string filename = string.Format("{0}-{1}-{2}", "CustomUpdatesFromASP.aspx.cs", DateTime.Now.ToString("MM-dd-yy"), sUniqueName);
        Response.Clear();
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
        workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8);
        Response.End();
}

After the Response.End() i.e. after the download is complete, I tried to use javascript to close the window but it did not work.

the javascript function I used after Response.End() is something like this.
C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "javascript:window.close();", true);


Please help me with this situation.

Thanks
Posted
v2

1 solution

First Solution
Response.End() stops the execution of the page after that.
So, the script is not going to execute.
Just remove that and try.

Second Solution
Quote:
one solution, kludgey, but can be used elsewhere:

C#
Response.Redirect("close.html")


where close.html just has

JavaScript
<script>
window.close();
</script>
Refer - closing a window after Response.End[^]
Note - Remove Response.End() and try this.
 
Share this answer
 
v2
Comments
bishnu karki 5-Apr-13 2:25am    
I removed that Response.End part but it didnot change anything. Any other suggestion Tadit Dast.
Try with the second solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900