Hi,
I have a LinkButton within update panel and on button click i want to download xlsx file from server.
But due to update panel file download is not happening.
This is my code.
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
string[] ar = btn.CommandArgument.ToString().Split('|');
MTemptyxlxs();
DataSet ds = new DataSet();
using (MySqlConnection con =
new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
using (MySqlCommand cmd =
new MySqlCommand("sp_gv", con))
{
cmd.CommandTimeout = 2147483;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@spAAA", ar[0].ToString());
cmd.Parameters.AddWithValue("@spBBB", ar[1].ToString());
cmd.Parameters.AddWithValue("@spCCC", ar[2].ToString());
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
ExportDataSetToExcel(ds);
}
}
}
public static void ExportDataSetToExcel(DataSet ds)
{
string AppLocation = @"D:\inetpub\wwwroot\aspnet\";
string guid = Guid.NewGuid().ToString().ToUpper().Replace("-", "_");
HttpContext.Current.Response.Cookies["guid"].Value = guid.ToString();
HttpContext.Current.Response.Cookies["guid"].Expires = DateTime.Now.AddMinutes(3);
string filepath = AppLocation + "Export_" +
DateTime.Now.ToString("ddMMyyyyHHmm") + "_" +
HttpContext.Current.Response.Cookies["guid"].Value + ".xlsx";
using (XLWorkbook wb = new XLWorkbook())
{
for (int i = 0; i < ds.Tables.Count; i++)
{
wb.Worksheets.Add(ds.Tables[i], ds.Tables[i].TableName);
}
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
wb.SaveAs(filepath);
wb.Dispose();
}
Thread.Sleep(3000);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/force-download";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Export_" +
DateTime.Now.ToString("ddMMyyyyHHmm") + "_" +
HttpContext.Current.Response.Cookies["guid"].Value + ".xlsx");
HttpContext.Current.Response.TransmitFile(@"D:\inetpub\wwwroot\aspnet\Export_" +
DateTime.Now.ToString("ddMMyyyyHHmm") + "_" +
HttpContext.Current.Response.Cookies["guid"].Value + ".xlsx");
HttpCookie cookie = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
HttpContext.Current.Response.AppendCookie(cookie);
HttpContext.Current.Response.End();
}
<asp:TemplateField
ItemStyle-HorizontalAlign="Center"
ItemStyle-CssClass="ddl_Class_new"
HeaderText="Nr.">
<ItemTemplate>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="modal">
<div class="center">
<img alt="" src="/aspnet/img/ajax-loader.gif" />
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton2" />
</Triggers>
<ContentTemplate>
<asp:LinkButton ID="LinkButton2" runat="server"
CssClass="ddl_Class_new"
OnClick="LinkButton2_Click"
CommandArgument='<%# Eval("AAA") + "|" + Eval("BBB") + "|" + Eval("CCC") %>'
Text='<%# Eval("Cr") %>'></asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>