2013-05-07 154 views
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(); 
     } 
    } 
+0

發表您的DoServerMethodProcess()代碼 – 2013-05-07 13:58:47

+0

我轉貼的代碼和將DoServerMethodProcess代碼更改爲實際將單詞doc轉換爲pdf的實際代碼。 – AdamTheITMan 2013-05-07 14:07:56

+0

ScriptManager.RegisterStartupScript(this,this.GetType(),「whatever」,「alert('hello')」,true);代碼不會等待..直到ConvertToPDF()完成.. – 2013-05-07 14:11:02

回答

0

由於導出PDF將數據發送到輸出流,有效地防止了正常的頁面生命週期,這是一個有點不是那麼複雜。我這裏還有基本的步驟,以達到預期的效果:

  1. Button1ClientClick事件揭開序幕setInterval功能,將定期檢查出口之後的特定cookie值
  2. 在服務器端代碼是完全使用響應.AppendCookie發送cookie。
  3. 回到客戶端的setInteval函數收到預期的餅乾,這就是你的標誌,以執行additionalclient端代碼(警告用戶,隱藏進度indidicator等)
+0

我發現在我的代碼的洋蔥深處,有一個調用「Response.flush()和Response.End()」這些都看似結束了JavaScript的執行。當我替換響應命令時,我能夠執行javascript。感謝大家的時間。 – AdamTheITMan 2013-05-08 13:13:04