有人请帮助我理解为什么 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();
}
}