moss里Response.End() 后页面按钮等功能全部失效如何解决?
如题;
下面是excel下载代码:
Response.Write(" <script>window.opener=null;window.close(); </script>");
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", Server.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd") + ".xls")));
Response.Flush();
Response.BinaryWrite(DataToExcel.SaveDataTableToExcel(dt));
//DataToExcel.SaveDataTableToExcel(dt)把数据源转换成byte[]
另外:在moss的环境下,用IE8有这个问题,但是在普通环境里用IE6没有这个问题存在,不知道是moss的问题,还是IE的问题。。。
网上找到一个解决方案,但是不能用呢,方案如下:
说正题:
我要说的这个问题,在我的项目中是这样发生的,因为项目需要,需要在页面中对数据进行导出为一个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,我们通过按钮来调用方法重置
这个参数的值,达到系统检索页面时,一直默认为未提交状态。