简单实用的防止多次提交辅助类

  一:  开题

    这篇只是一个简单的应用技巧,高手请跳过,别拍砖,打击我这个有激情的菜鸟。在我们的web项目中经常会遇见由于网络原为等问题,而导致在页面提高后,服务器还没有来得及返回的时候,我们的用户可能在此点击按钮使的数据多次的提交。防止这个行为很简单,我们一般经常是在按钮点击后使其禁用disabled=true。我是一个很懒的人(生活中并不懒,只是写Code比较懒,我的目标是:少写Code,基于配置而不写Code是最好的3]6GJ(EWN[K2P[Z6B`6B`2H  )。所以就有了这个简单的辅助类:我的思路是在page的PreRender时间中注册提交前执行的脚本全部提交按钮的disabled=true(  page.ClientScript.RegisterOnSubmitStatement()。)等待服务器返回时会更具返回浏览器会重绘,所以我们的设置此时已经无用了。呵呵就这面简单。但是为不少写代码,那些了一个辅助类DoubleSubmitPrevent,提供的几种方式自动按照按钮,或者是手动添加按钮(取决于IsAutoFind),自动查找的起点默认为page,但是为了效率你可以自己设置BaseContrlForFind,关于需要禁止的按钮的判断为IsPreventControl你可以自己定义覆盖默认的,默认为:

 
 
  1. 代码   
  2.  
  3. private System.Predicate<System.Web.UI.Control> isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||   
  4.                        (t is System.Web.UI.WebControls.LinkButton) ||   
  5.                        (t is System.Web.UI.WebControls.ImageButton) ||   
  6.                        (t is System.Web.UI.HtmlControls.HtmlButton) ||   
  7.            //(t is System.Web.UI.HtmlControls.HtmlLink) ||   
  8.                        (t is System.Web.UI.HtmlControls.HtmlInputButton) ||   
  9.                        (t is System.Web.UI.HtmlControls.HtmlInputSubmit);   

如果你还是觉得效率不好那么,你就可以自己Add或者AddRange,同时包括了Remove,Insert等方法,这一系列方法都支持链式操作(这是第一次使用jQuery的时候给我的最大触动)。例如:dsp.Add(Button1).Add(Button2).Add(Button3).Add(LinkButton1).Add(LinkButton2) =dsp.AddRange(Button1,Button2,Button3,LinkButton1,LinkButton2);包括的多个重载;

  二: Code部分

说了这么多还是直接上代码:

 
 
  1. 代码   
  2.  
  3. namespace Wolf.Utils   
  4. {   
  5.     public class DoubleSubmitPrevent   
  6.     {   
  7.         private System.Collections.Generic.List<string> _clientIDList = null;   
  8.         private const string DOUBLE_SUBMIT_PREVENT_STR = "B3F6F682-F404-4519-9F30-79876E5A5C9A_WOLF_DOUBLESUBMITPREVENT_391B8D4F-757E-4005-8262-062652D8BAC6";   
  9.         private bool isAutoFind = false;   
  10.         #region judje  Prevent Control?   
  11.         private System.Predicate<System.Web.UI.Control> isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||   
  12.                         (t is System.Web.UI.WebControls.LinkButton) ||   
  13.                         (t is System.Web.UI.WebControls.ImageButton) ||   
  14.                         (t is System.Web.UI.HtmlControls.HtmlButton) ||   
  15.             //(t is System.Web.UI.HtmlControls.HtmlLink) ||   
  16.                         (t is System.Web.UI.HtmlControls.HtmlInputButton) ||   
  17.                         (t is System.Web.UI.HtmlControls.HtmlInputSubmit);   
  18.         #endregion   
  19.         private System.Web.UI.Control baseContrlForFind = null;  
  20.  
  21.         /// <summary>   
  22.         /// Auto Find will satrt with this Control;Default this Page .   
  23.         /// </summary>   
  24.         public System.Web.UI.Control BaseContrlForFind   
  25.         {   
  26.             get { return baseContrlForFind; }   
  27.             set { baseContrlForFind = value; }   
  28.         }  
  29.  
  30.         /// <summary>   
  31.         /// judje the Contrl that be prevented;   
  32.         /// </summary>   
  33.         public System.Predicate<System.Web.UI.Control> IsPreventControl   
  34.         {   
  35.             get { return isPreventControl; }   
  36.             set { isPreventControl = value; }   
  37.         }   
  38.         /// <summary>   
  39.         /// Auto Find the Control that be prevented ?   
  40.         /// </summary>   
  41.         public bool IsAutoFind   
  42.         {   
  43.             get { return isAutoFind; }   
  44.             set { isAutoFind = value; }   
  45.         }  
  46.  
  47.         public DoubleSubmitPrevent(System.Web.UI.Page page)   
  48.         {   
  49.             _clientIDList = new System.Collections.Generic.List<string>();   
  50.             baseContrlForFind = page;   
  51.             page.PreRenderCompleted += new System.EventHandler(DoubleSubmitPreventPagePreRenderHick);   
  52.         }  
  53.  
  54.         public DoubleSubmitPrevent Add(string clientID)   
  55.         {   
  56.             _clientIDList.Add(clientID);   
  57.             return this;   
  58.         }   
  59.         public DoubleSubmitPrevent Add(System.Web.UI.Control ctr)   
  60.         {   
  61.             _clientIDList.Add(ctr.ClientID);   
  62.             return this;   
  63.         }  
  64.  
  65.         public DoubleSubmitPrevent AddRange(params string[] clientIDs)   
  66.         {   
  67.             _clientIDList.AddRange(clientIDs);   
  68.             return this;   
  69.         }  
  70.  
  71.         public DoubleSubmitPrevent AddRange(params System.Web.UI.Control[] ctrs)   
  72.         {   
  73.             foreach (var item in ctrs)   
  74.             {   
  75.                 Add(item);   
  76.             }   
  77.             return this;   
  78.         }  
  79.  
  80.         public DoubleSubmitPrevent Remove(string clientID)   
  81.         {   
  82.             _clientIDList.Remove(clientID); return this;   
  83.         }  
  84.  
  85.         public DoubleSubmitPrevent Remove(System.Web.UI.Control ctr)   
  86.         {   
  87.             _clientIDList.Remove(ctr.ClientID);   
  88.             return this;   
  89.         }  
  90.  
  91.         public bool Exists(string clientID)   
  92.         {   
  93.             return _clientIDList.Exists(t => t.Equals(clientID));   
  94.         }  
  95.  
  96.         public bool Exists(System.Web.UI.Control ctr)   
  97.         {   
  98.             return _clientIDList.Exists(t => t.Equals(ctr.ClientID));   
  99.         }  
  100.  
  101.         protected virtual void DoubleSubmitPreventPagePreRenderHick(object sender, System.EventArgs e)   
  102.         {   
  103.             System.Web.UI.Page page = sender as System.Web.UI.Page;   
  104.             if (page != null)   
  105.             {  
  106.  
  107.                 if (isAutoFind)   
  108.                 {   
  109.                     #region Find Action   
  110.                     System.Action<System.Collections.Generic.List<string>, System.Web.UI.Control> action = null;   
  111.                     action = (list, ctr) =>   
  112.                     {   
  113.                         if (ctr != null)   
  114.                         {   
  115.                             if (isPreventControl(ctr))   
  116.                             {   
  117.                                 list.Add(ctr.ClientID);   
  118.                             }   
  119.                             foreach (System.Web.UI.Control item in ctr.Controls)   
  120.                             {   
  121.                                 action(list, item);   
  122.                             }   
  123.                         }  
  124.  
  125.                     };   
  126.                     #endregion   
  127.                     action(_clientIDList, baseContrlForFind);  
  128.  
  129.                 }  
  130.  
  131.                 System.Text.StringBuilder sb = new System.Text.StringBuilder();   
  132.                 foreach (var item in _clientIDList)   
  133.                 {   
  134.                     sb.Append(string.Format(" document.getElementById(\"{0}\").disabled=true;", item));   
  135.                 }   
  136.                 page.ClientScript.RegisterOnSubmitStatement(this.GetType(), DOUBLE_SUBMIT_PREVENT_STR, sb.ToString());   
  137.             }   
  138.         }   
  139.     }   
  140. }  
  141.  
  142.  
  143. 复制代码 

三: 测试:

为了模拟延时,我在后台加了睡眠:

 
 
  1. 代码   
  2. protected void Page_Load(object sender, EventArgs e)   
  3.    {   
  4.        if (IsPostBack)   
  5.        {   
  6.            System.Threading.Thread.Sleep(1000 * 5);   
  7.            TextBox1.Text = DateTime.Now.ToString();   
  8.            Button3.Enabled = false;   
  9.        }   
  10.        Wolf.Utils.DoubleSubmitPrevent dsp = new Wolf.Utils.DoubleSubmitPrevent(this) { IsAutoFind = true, BaseContrlForFind = this.form1 };   
  11.        //dsp.Add(Button1).Add(Button2).Add(Button3).Add(LinkButton1).Add(LinkButton2);   
  12.          
  13.    }   

 前台html(乱托乱扔的,看来是比较懒8{U`QQB5X27@C_FO](KQ(4G  ):

 
 
  1. 代码   
  2. <form id="form1" runat="server">   
  3.     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
  4.     <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />   
  5.      <asp:Button ID="Button2" runat="server" Text="Button" />   
  6.       <asp:Button ID="Button3" runat="server" Text="Button" />   
  7.     <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><asp:LinkButton   
  8.         ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>   
  9.     </form>  
  10.  

效果:

如果你又更好的方案,也希望能给我分享,请大家多多指教。






 本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/834773,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值