您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“在ASP.NET中如何防止頁(yè)面多次提交”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
此處提供的代碼用來(lái)實(shí)現(xiàn)當(dāng)asp.net頁(yè)面中的某個(gè)Button被點(diǎn)擊后disable掉該頁(yè)面中所有的Button,從而防止提交延時(shí)導(dǎo)致的多次提交。基于之前的onceclickbutton腳本.
//ASP.NET中防止頁(yè)面多次提交的代碼:javascript< 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>
//ASP.NET中防止頁(yè)面多次提交的代碼:asp.netpublic 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) 正確注冊(cè) if(!IsPostBack) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }}"); //保證驗(yàn)證函數(shù)的執(zhí)行 sb.Append("if(window.confirm('are you sure?')==false) return false;"); //自定義客戶端腳本 sb.Append("disableOtherSubmit();"); // disable所有submit按鈕 sb.Append(this.GetPostBackEventReference(this.Button3)); //用__doPostBack來(lái)提交,保證按鈕的服務(wù)器端click事件執(zhí)行 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, 我覺(jué)得其它的可提交控件也是可以通過(guò)類似的方法來(lái)disable的.
“在ASP.NET中如何防止頁(yè)面多次提交”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。