“文件下载”弹出窗口将覆盖警报弹出窗口

时间:2012-09-13 14:30:58

标签: c# javascript asp.net

当用户点击“导出到Excel”链接时,会向用户显示标准的“文件下载”对话框。 See here for an example image

但在导出excel文件之前,我想显示一个警告弹出窗口。但是“保存”对话框模糊了警报弹出窗口的视图。

如何在不被遮挡的情况下显示弹出窗口?

这是我的代码......

dsResult = clsObj.getSearchResults_BL(detObj);

if (OrdDifference != null && OrdDifference.Any())
{
   ScriptManager.RegisterClientScriptBlock(this.up, this.GetType(), "export", "alert('.....')", true);
   set(dsResult, strName);
}
else
{
  set(dsResult, strName);
}

private void set(DataSet ds, string strFileName)
{
    ExcelEngine excelEngine = new ExcelEngine();
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Excel2007;
    IWorkbook workbook = application.Workbooks.Create(1);
    IWorksheet sheet = workbook.Worksheets[0];
    try
    {
        sheet.Name = strFileName;
        sheet.ImportDataTable(ds.Tables[0], true, 1, 1, -1, -1);

        ...

        workbook.SaveAs(strFileName, ExcelSaveType.SaveAsXLS, HttpContext.Current.Response, ExcelDownloadType.PromptDialog);

    }
    catch (Exception ex)
    {

    }
}

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

ScriptManager.RegisterClientScriptBlock(this.up, this.GetType(), "export", "alert('.....')", true);
set(dsResult, strName);

由于程序中的set方法正在写入响应流,因此对ScriptManager.RegisterClientScriptBlock的调用最终无效。

您需要分两步执行此操作:

if (OrdDifference != null && OrdDifference.Any())
{
   //Just do this, nothing more.
   ScriptManager.RegisterClientScriptBlock(this.up, this.GetType(), "export", "alertUser('Some Message Here')", true);

}

现在在Javascript中定义alertUser函数:

function alertUser(message)
{
    alert(message);
    window.location='AccepServiceOrder.aspx?o=Export';
}

现在在Page_Load上检查查询字符串中的o参数

protected void Page_Load(object sender, EventArgs e)
{
     if(Request.QueryString["o"]!=null)
     { 
        dsResult = clsObj.getSearchResults_BL(detObj);
         set(dsResult, strName);
     }
}