1

有人请帮助我理解为什么 RegisterStartupScript 在某些同时执行服务器代码的情况下不起作用。例如,假设我有 button1,并且 onclick 事件看起来像这样:

protected void Button1_Click(object sender, EventArgs e) {
  ScriptManager.RegisterStartupScript(this, this.GetType(), "whatever", "alert('hello')", true);
}

这将毫无问题地执行,我会收到一个弹出窗口说“你好”。但是,如果我有类似的东西:

protected void Button1_Click(object sender, EventArgs e) {
  ConvertToPDF(pdfFileName, pdfFilePath);
  ScriptManager.RegisterStartupScript(this, this.GetType(), "whatever", "alert('hello')", true);
}

这将不会执行——因为似乎是因为在 javascript 事件之前还有一个服务器事件也触发了。

然而,这将起作用:

protected void Button1_Click(object sender, EventArgs e) {
  Session("MySessionVariable") = textBox1.Text;
  ScriptManager.RegisterStartupScript(this, this.GetType(), "whatever", "alert('hello')", true);
}

分配会话变量不使用服务器处理,那么为什么会这样呢?

这是似乎杀死事情的 ConvertToPDF 方法:

   public void ConvertToPDF(string pdfFileName, string docPath)
    {
        ApplicationClass wordApplication = new ApplicationClass();
        Microsoft.Office.Interop.Word.Document wordDocument = null;

        object paramSourceDocPath = docPath;
        object paramMissing = System.Reflection.Missing.Value;
        object isVisible = true;
        object isReadonly = false;

        string paramExportFilePath = HttpContext.Current.Server.MapPath(
            ConfigurationManager.AppSettings["Docs"] + pdfFileName + ".pdf");
        WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

        bool paramOpenAfterExport = false;
        WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
        WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

        int paramStartPage = 0;
        int paramEndPage = 0;

        WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
        bool paramIncludeDocProps = true;

        bool paramKeepIRM = true;
        WdExportCreateBookmarks paramCreateBookmarks =

        WdExportCreateBookmarks.wdExportCreateWordBookmarks;
        bool paramDocStructureTags = true;

        bool paramBitmapMissingFonts = true;
        bool paramUseISO19005_1 = false;

        try
        {

            // Open the source document.

            wordDocument = wordApplication.Documents.Open(

            ref paramSourceDocPath, ref paramMissing, ref isReadonly,
            ref paramMissing, ref paramMissing, ref paramMissing,

            ref paramMissing, ref paramMissing, ref paramMissing,
            ref paramMissing, ref paramMissing, ref paramMissing,

            ref isVisible, ref paramMissing, ref paramMissing,
            ref paramMissing);

            // Export it in the specified format.

            if (wordDocument != null)
                wordDocument.ExportAsFixedFormat(paramExportFilePath,

                paramExportFormat, paramOpenAfterExport,

                paramExportOptimizeFor, paramExportRange, paramStartPage,

                paramEndPage, paramExportItem, paramIncludeDocProps,

                paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

                paramBitmapMissingFonts, paramUseISO19005_1,

                ref paramMissing);
        }
        catch (Exception ex)
        {
          // logging code
        }
        finally
        {
            // Close and release the Document object.

            if (wordDocument != null)
            {
                wordDocument.SaveAs(ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);

                ((Microsoft.Office.Interop.Word._Document)wordDocument).Close(ref paramMissing, ref paramMissing,
                 ref paramMissing);

                wordDocument = null;
            }

            // Quit Word and release the ApplicationClass object.

            if (wordApplication != null)
            {

                wordApplication.Quit(ref  paramMissing, ref paramMissing,
                ref paramMissing);

                wordApplication = null;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect(); GC.WaitForPendingFinalizers();
        }
    }
4

1 回答 1

0

由于导出 PDF 将数据发送到输出流,有效地阻止了正常的页面生命周期,因此比这有点复杂。以下是达到预期效果的基本步骤:

  1. Button1 ClientClick事件启动setInterval功能上,将定期检查特定的 cookie 值
  2. 在导出完成后的服务器端代码上,使用 Response.AppendCookie 发送 cookie。
  3. 返回客户端,setInteval 函数接收预期的 cookie,这是您执行附加客户端代码的标志(提醒用户、隐藏进度指示器等)
于 2013-05-07T19:22:47.940 回答