在C#中保存工作簿后添加脚本块

时间:2015-04-13 08:01:55

标签: c# asp.net aspose

我有下面的代码,它会创建一个aspose excel文件来弹出。我需要一个脚本块,以便在下载发生后立即从javascript执行。在以下代码中没有触发registerstartupscript。

这有什么问题?

workbook.Save(HttpContext.Current.Response, "Template.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));


Page.RegisterStartupScript("alert", "<script>parent.downloadcomplete()</script>");

1 个答案:

答案 0 :(得分:0)

使用ClientScriptManager的实例。

workbook.Save(HttpContext.Current.Response, "Template.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
// Page.RegisterStartupScript("alert", "<script>parent.downloadcomplete()</script>");

ClientScriptManager cScript = Page.ClientScript;
if (!cScript.IsStartupScriptRegistered(this.GetType(), "alert"))
{
    // Register if not done already
    string javascriptCode = "<script type='text/javascript'> alert('Download complete'); </script>";

    cScript.RegisterStartupScript(this.GetType(), "alert", javascriptCode);
}