防止多次提交的几个比较
生活随笔
收集整理的這篇文章主要介紹了
防止多次提交的几个比较
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、利用Session(viewState是不行的,viewState要回傳才能讀到數(shù)據(jù))
如果有個數(shù)據(jù)包類StockBillMP,則設定
? ????????private?StockBillMP?M_saveobj
????????{
????????????get
????????????{
????????????????return?(Session["saveobj"]?==?null)?null:(StockBillMP)Session["saveobj"];
????????????}
????????????set
????????????{
????????????????Session["saveobj"]?=?value;
????????????}
????????}????
然后用比較函數(shù)
? ????????private?bool?checkIsRepeate(StockBillMP?objRet)
????????{
????????????bool?bret?=?true;??//假設是重復的,不重復就退出
????????????
????????????for(int?i=1;i<objRet.objMainProc.arrParams.Length;i++)
????????????{
????????????????string?strOld?=?M_saveobj.objMainProc.arrParams[i].Value.ToString().Trim();
????????????????string?strNew?=?objRet.objMainProc.arrParams[i].Value.ToString().Trim();
????????????????if?(strOld?!=?strNew)
????????????????{
????????????????????bret?=?false;
????????????????????break;
????????????????}
????????????}
????????????if?(bret)
????????????{
????????????????for(int?i=0;i<objRet.arrSubProc.Length;i++)
????????????????{
????????????????????for(int?j=1;j<objRet.arrSubProc[i].arrParams.Length;j++)
????????????????????{
????????????????????????string?strOld?=?M_saveobj.arrSubProc[i].arrParams[j].Value.ToString().Trim();
????????????????????????string?strNew?=?objRet.arrSubProc[i].arrParams[j].Value.ToString().Trim();
????????????????????????if?(strOld?!=?strNew)
????????????????????????{
????????????????????????????bret?=?false;
????????????????????????????break;
????????????????????????}
????????????????????????bret?=?true;
????????????????????}
????????????????????if(!bret)
????????????????????????break;
????????????????}
????????????}?????????????????????????????????????
????????????
????????????return?bret;
????????}
保存成功就更改Session
M_saveobj?=?objRet;
代價是慘重的,效果還可以,整個包啊
當然僅僅是實現(xiàn)防止提交速度慢,連續(xù)點擊的情況是設定一個bool變量就可以了,效果如2檢查按鈕。
2、檢查按鈕
http://bestcomy.cnblogs.com/archive/2005/10/10/251507.html
此處提供的代碼用來實現(xiàn)當asp.net頁面中的某個Button被點擊后disable掉該頁面中所有的Button,從而防止提交延時導致的多次提交?;谥暗膐nceclickbutton腳本.
<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;?}}");????//保證驗證函數(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來提交,保證按鈕的服務器端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, 我覺得其它的可提交控件也是可以通過類似的方法來disable的.
如果有個數(shù)據(jù)包類StockBillMP,則設定
? ????????private?StockBillMP?M_saveobj
????????{
????????????get
????????????{
????????????????return?(Session["saveobj"]?==?null)?null:(StockBillMP)Session["saveobj"];
????????????}
????????????set
????????????{
????????????????Session["saveobj"]?=?value;
????????????}
????????}????
然后用比較函數(shù)
? ????????private?bool?checkIsRepeate(StockBillMP?objRet)
????????{
????????????bool?bret?=?true;??//假設是重復的,不重復就退出
????????????
????????????for(int?i=1;i<objRet.objMainProc.arrParams.Length;i++)
????????????{
????????????????string?strOld?=?M_saveobj.objMainProc.arrParams[i].Value.ToString().Trim();
????????????????string?strNew?=?objRet.objMainProc.arrParams[i].Value.ToString().Trim();
????????????????if?(strOld?!=?strNew)
????????????????{
????????????????????bret?=?false;
????????????????????break;
????????????????}
????????????}
????????????if?(bret)
????????????{
????????????????for(int?i=0;i<objRet.arrSubProc.Length;i++)
????????????????{
????????????????????for(int?j=1;j<objRet.arrSubProc[i].arrParams.Length;j++)
????????????????????{
????????????????????????string?strOld?=?M_saveobj.arrSubProc[i].arrParams[j].Value.ToString().Trim();
????????????????????????string?strNew?=?objRet.arrSubProc[i].arrParams[j].Value.ToString().Trim();
????????????????????????if?(strOld?!=?strNew)
????????????????????????{
????????????????????????????bret?=?false;
????????????????????????????break;
????????????????????????}
????????????????????????bret?=?true;
????????????????????}
????????????????????if(!bret)
????????????????????????break;
????????????????}
????????????}?????????????????????????????????????
????????????
????????????return?bret;
????????}
保存成功就更改Session
M_saveobj?=?objRet;
代價是慘重的,效果還可以,整個包啊
當然僅僅是實現(xiàn)防止提交速度慢,連續(xù)點擊的情況是設定一個bool變量就可以了,效果如2檢查按鈕。
2、檢查按鈕
http://bestcomy.cnblogs.com/archive/2005/10/10/251507.html
此處提供的代碼用來實現(xiàn)當asp.net頁面中的某個Button被點擊后disable掉該頁面中所有的Button,從而防止提交延時導致的多次提交?;谥暗膐nceclickbutton腳本.
<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;?}}");????//保證驗證函數(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來提交,保證按鈕的服務器端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, 我覺得其它的可提交控件也是可以通過類似的方法來disable的.
總結
以上是生活随笔為你收集整理的防止多次提交的几个比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 關于招聘新人
- 下一篇: Community Server系列之四