防止頁面重複提交代碼(轉帖)

來源:互聯網
上載者:User
此處提供的代碼用來實現當asp.net頁面中的某個Button被點擊後disable掉該頁面中所有的Button,從而防止提交延時導致的多次提交。基於之前的onceclickbutton指令碼.

<script language="javascript">
        <!--
        
        function disableOtherSubmit()
        {            var obj = event.srcElement;
            var objs = document.getElementsByTagName('INPUT');
            for(var i=0; i<objs.length; i++)
            {
                if(objs[i].type.toLowerCase() == 'submit')
                {
                    objs[i].disabled = true;
                }
            }        }
            
        //-->
        </script>public class PreventMultiClick : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.WebControls.LinkButton LinkButton1;
        protected System.Web.UI.WebControls.Button Button3;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            this.GetPostBackEventReference(this.Button3);        //保證 __doPostBack(eventTarget, eventArgument) 正確註冊
            if(!IsPostBack)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }}");    //保證驗證函式的執行
                sb.Append("if(window.confirm('are you sure?')==false) return false;");        //自訂用戶端指令碼
                sb.Append("disableOtherSubmit();");        // disable所有submit按鈕
                sb.Append(this.GetPostBackEventReference(this.Button3));    //用__doPostBack來提交,保證按鈕的伺服器端click事件執行
                sb.Append(";");
                Button3.Attributes.Add("onclick",sb.ToString());
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.Button3.Click += new System.EventHandler(this.Button3_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button3_Click(object sender, System.EventArgs e)
        {
            System.Threading.Thread.Sleep(3000);
            Response.Write("Hello world!");
        }
    }

此處只是disable掉所有的submit button, 我覺得其它的可提交控制項也是可以通過類似的方法來disable的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.