SharePoint工程中用Response生成Excel以后页面按钮失效问题

 

 在按钮事件中添加属性:OnClientClick="_spFormOnSubmitCalled=false“ 即可。

<asp:Button ID="Searsh" runat="server" Text="Search"  Width="50px" OnClientClick="_spFormOnSubmitCalled=false"
                                                  onclick="Searsh_Click"   />

 

一共收集了四种解决方案不知道哪些方案在你的工程中适用:

1 在 page_load 里加上如下代码:

 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;”; 

使用这种方法有两个问题需要注意:(1)如果你的导出按键是写在UserControl里面那么 上面的方法会报错,因为RegisterClientScriptBlock将代码注册到源文件后面,用RegisterStartupScript的话,由于theForm.onsubmit 没有声明,会报错。(2)生成的excel文件如果选择”打开”,是很正常的;选择“保存”,页面上的所有脚本失效,前题还必须是target为_self错误信息为“未知错误“。个人分析认为这个是生成的页面虽然弹出打开,但设置的setHeader已经默认为excel文件格式,这个引响到了IE调用JS的方式(猜测),现在解决的方法为将target设为_blank,不过结果是会多一个白色的页面。

2  (1)原来的导出按钮增加 OnClientClick事件。
        btnExport.OnClientClick = “return AutoForm_Export(‘PostBackEventTarget_IsExport’);”;
     (2)客户端增加javascript函数AutoForm_Export。
        function AutoForm_Export(postBackEventTarget)
        {
           WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(postBackEventTarget, “”, true, “”, “”, false, true))    ;           
            return false;
        }
     (3)在服务器端根据Request["__EVENTTARGET"] 来判断是否是导出事件。注意下面红色字体的注释   

        if (Page.Request["__EVENTTARGET"] != null)
        {
                        bool isInAsyncPostBack = false;
                        ScriptManager sm = ScriptManager.GetCurrent(Page);
                        if (sm != null)
                        {
                            isInAsyncPostBack = sm.IsInAsyncPostBack;
                        }
                        if (isInAsyncPostBack )
                        {
                            /*
                            一定要进行判断是否是使用Axaj进行异步请求,否则在点击导出按钮以后,再进行其它Ajax请求, Page.Request["__EVENTTARGET"] 会保留原来的值,这样就错误判断了。
                            */
                            _isExport = false

                        }
                        else
                        {
                            string postBackEventTarget = Page.Request["__EVENTTARGET"].ToString();
                            if (postBackEventTarget == “PostBackEventTarget_IsExport”)
                            { 
                                _isExport = true;
                            }
                            else
                                _isExport = false;
                        }
                    }
                    else
                        _isExport = false;

            }

//我不知道这个” _isExport”变量是什么作用

3 这种方法比较简单明了,但它却有它的局限性,只能用在服务器端的按钮事件中(我个人觉得是适用在有OnClientClick事件的服务器控件上)所有按钮上加上一个客户端事件:OnClientClick=”_spFormOnSubmitCalled=false;”。

4 在Page_Load中注册两行Javascript脚本,string script = “_spOriginalFormAction = document.forms[0].action;\n_spSuppressFormOnSubmitWrapper = true;”; 

this.ClientScript.RegisterClientScriptBlock(this.GetType(), “script”, script, true); 

可能是受其它因素的影响吧,只有第四种方法奏效。其它方法期待你的验证!

(Visited 19 times, 1 visits today)

 

posted @ 2012-09-05 10:55  邑尘  阅读(786)  评论(0编辑  收藏  举报