Popup opens to get details in a form from the user. User enters the details then click download button to download the excel file. File is downloaded but div (or dialog) is not closed (even with the code to close it) due to the Response.Flush() / Response.End(). No other code works after these two Response to close the div. Could not find any solution online.
Aspx file Div:
<div id="poppage" runat="server">
Javascript Code to open Div:
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
function showpage1() {
var mydiv = $('#poppage');
mydiv.dialog({
autoOpen: false, modal: true, width: '30%',
position: { my: 'top', at: 'top+150' }
});
mydiv.load('DownloadForm.aspx');
// Open the dialog
mydiv.dialog('open');
}
</script>
C# Code to download the file:
protected void DownloadExcel()
{
DataSet ds = new DataSet();
ds.ReadXml("C:\CONFIDENTIAL\Development\XMLFile.xml");
DataTable dt = ds.Tables[0];
ExcelPackage package = new ExcelPackage();
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestWS");
//Add the headers
sheet.Cells[1, 1].Value = "CustomerName";
sheet.Cells[1, 2].Value = "Description";
int ctr = 0;
foreach (DataRow row in dt.Rows)
{
ctr += 1;
sheet.Cells[ctr + 1, 1].Value = row["CustomerName"].ToString();
sheet.Cells[ctr + 1, 2].Value = row["Description"].ToString();
}
sheet.Cells[1, 1, ctr + 1, 14].Style.Border.Left.Style = ExcelBorderStyle.Thin;
sheet.Cells[1, 1, ctr + 1, 14].Style.Border.Right.Style = ExcelBorderStyle.Thin;
sheet.Cells[1, 1, 1, 14].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
sheet.Cells[1, 1, 1, 14].Style.Border.Top.Style = ExcelBorderStyle.Thin;
sheet.Cells[ctr + 1, 1, ctr + 1, 14].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
for (int i = 1; i <= sheet.Dimension.End.Column; i++)
{
sheet.Column(i).Width = 14;
}
for (int i = 1; i <= sheet.Dimension.End.Row; i++)
{
sheet.Row(i).Height = 146;
}
sheet.Cells[1, 1, 14, 14].Style.WrapText = true;
var allCells = sheet.Cells[1, 1, sheet.Dimension.End.Row, sheet.Dimension.End.Column];
var cellFont = allCells.Style.Font;
cellFont.SetFromFont(new Font("Arial", 8));
sheet.Cells[1, 1, 1, 14].Style.Font.Bold = true;
sheet.Cells["A1:N1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
sheet.Cells["A1:N1"].Style.Fill.BackgroundColor.SetColor(Color.CornflowerBlue);
sheet.Row(1).Height = 12;
sheet.Column(14).Width = 20;
package.Workbook.Properties.Title = "MyCompany";
package.Workbook.Properties.Author = "CompanyAuthor";
package.Workbook.Properties.Comments = "This is a system generated file";
package.Workbook.Properties.Company = "Test Company";
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
byte[] buffer = package.GetAsByteArray();
Response.BufferOutput = true;
Response.AddHeader("Accept-Ranges", "bytes");
Response.AddHeader("Accept-Header", buffer.Length.ToString());
Response.AddHeader("Cache-Control", "public");
Response.AddHeader("Cache-Control", "must-revalidate");
Response.AddHeader("Pragma", "public");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename="Excelfile-" + DateTime.Now.ToString("ddMMyyyyHHmm") + ".xlsx" + """);
Response.AddHeader("expires", "0");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}