有人请帮助我理解为什么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();
}
}
由于导出PDF将数据发送到输出流,有效地阻止了正常的页面生命周期,因此比这稍微复杂一些。以下是达到预期效果的基本步骤:
- 在
Button1
ClientClick事件启动setInterval
功能,该功能将定期检查特定的cookie值 - 在导出完成后的服务器端代码中使用Response。AppendCookie发送cookie
- 返回到客户端,setinterval函数接收到预期的cookie,这是你执行额外的客户端代码的标志(警告用户,隐藏进度指示器等)