我要说的这个问题,在我的项目中是这样发生的,因为项目需要,需要在页面中对数据进行导出为一个excel文件。执行代码如下:
this.Page.Response.ClearContent();
this.Page.Response.ContentEncoding = System.Text.Encoding.UTF8;
this.Page.Response.ContentType = "application/vnd.ms-excel; charset=utf-8";
this.Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + reportname + DateTime.Today.ToString("MMddyyyy") + ".xls");
this.Page.Response.Write(ExportUtil.DataTableToExcelTable(dt, displayColumnNames, null));
this.Page.Response.End();
运行成功,但是页面上的导出按钮不能再次点击。
后搜寻国内外网站,都说要用Popup的方式来解决。很气馁。终于在一个国外的论坛里发现了解决办法。
现将解决方式如下:
string beforeSubmitJS = "/nvar exportRequested = false; /n";
beforeSubmitJS += "var beforeFormSubmitFunction = theForm.onsubmit;/n";
beforeSubmitJS += "theForm.onsubmit = function(){ /n";
beforeSubmitJS += "var returnVal = beforeFormSubmitFunction(); /n";
beforeSubmitJS += "if(exportRequested && returnVal) {_spFormOnSubmitCalled=false; exportRequested=false;} /n";
beforeSubmitJS += "return returnVal; /n";
beforeSubmitJS += "}; /n";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true);
注册javascript程序,然后再按钮提交时发送一个参数:
this._btnSubmit.Attributes["onclick"] = "javascript:exportRequested=true;";
这样可以解决这个问题。
原理:页面按钮提交时会调用一个内置的方法并修改一个内置变量_spFormOnSubmitCalled,我们通过按钮来调用方法重置
这个参数的值,达到系统检索页面时,一直默认为未提交状态。