refresh après export Excel avec HttpContext.Current.Response
Bonjour,
Pour placer le contexte, j'ai une petite interface avec 2 paramètres à choisir puis un bouton qui va lancer le traitements des données que j'enregistre dans une base SQL server. Puis à la fin du traitement, je transfert des données de la base SQL via une requête incluant les paramètres dans une datatable, que j'exporte vers le client sous Excel via cette méthode:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| private void ExporttoExcel(DataTable table)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=detail.xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
//sets font
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
//sets the table border, cell spacing, border color, font of the text, background, foreground, font height
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:11.0pt; font-family:Calibri;'> <TR>");
//am getting my grid's column headers
int columnscount = table.Columns.Count;
for (int j = 0; j < columnscount; j++)
{ //write in new column
HttpContext.Current.Response.Write("<Td>");
//Get column headers and make it as bold in excel columns
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(table.Columns[j].Caption);
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in table.Rows)
{//write in new row
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
try
{
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
catch(Exception e)
{
string msg = e.Message;
HttpContext.Current.Response.End();
}
} |
Pendant le traitement des données, j'affiche un petit élément dynamique javascript (type gif animé) que je lance via l'évènement "OnClientClick" de cette manière :
Code:
<asp:Button ID="btnCalculer" runat="server" Text="Calculer" Width="122px" OnClick="btnCalculer_Click" OnClientClick="javascript:showWait();" />
J'ai mon bouton dans une div nommé id="lebouton" et mon élément indiquant le traitement en cours dans une div nommé id="patienter". Voici mes 2 fonctions en javascript:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <script lang="javascript" type="text/javascript">
function showWait() {
var opts = {
lines: 11, // The number of lines to draw
length: 5, // The length of each line
width: 3, // The line thickness
radius: 7, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 12, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#000', // #rgb or #rrggbb or array of colors
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: 2, // Top position relative to parent in px
left: 5 // Left position relative to parent in px
};
var target = document.getElementById('patienter');
var spinner = new Spinner(opts).spin(target);
target.style.display = '';
document.getElementById('lebouton').style.display = 'none';
}
function showBtn() {
var target = document.getElementById('patienter');
target.style.display = 'none';
document.getElementById('lebouton').style.display = '';
}
</script> |
Le téléchargement du fichier Excel se fait très bien pas de problème et mon élément dynamique s'affiche parfaitement bien.
Mon problème est qu'à la fin de l'export du fichier Excel, je n'arrive pas à cacher ma div "patienter" et afficher ma div "lebouton". D'après ce que j'ai pu lire, c'est à cause de la fonction "HttpContext.Current.Response.End();" mais je ne vois pas du tout comment contourner ça...
Ce que j'ai déjà essayé:
- ce code avant ou après l'export
Code:
Page.ClientScript.RegisterStartupScript(Page.GetType(), "showBtn", "<script language='javascript'> showBtn(); </script>");
- avant le End()
Code:
Response.Write("<script language='javascript'> showBtn(); </script>");
- remplacer Response.End() par Response.Close() et HttpContext.Current.ApplicationInstance.CompleteRequest();
Donc en gros après la fonction Response.End() je ne sais pas comment reprendre la main pour soit réafficher ou ma div ou même actualiser la page...
Quelqu'un pour m'orienter?