moss里Response.End() 后页面按钮等功能全部失效如何解决?

luoao9527 2009-09-16 03:26:20
如题;


下面是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,我们通过按钮来调用方法重置

这个参数的值,达到系统检索页面时,一直默认为未提交状态。
...全文
291 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongshanyu 2012-01-13
  • 打赏
  • 举报
回复
To birdzh
vs2003下好像没效果
birdzh 2009-10-28
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{
RepairCallBack();
}
private void RepairCallBack()
{
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.RegisterClientScriptBlock(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true);
butExport.Attributes["onclick"] = "javascript:exportRequested=true;";
}

butExport为导出Excel文件的Button;
RegisterClientScriptBlock将代码注册到源文件后面,RegisterStartupScript的话,由于theForm.onsubmit 没有声明,会报错
HugoWong 2009-09-17
  • 打赏
  • 举报
回复
收好
  • 打赏
  • 举报
回复
学习 。。。。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧