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

本文介绍了一种防止Web页面上的按钮被重复点击提交的方法。通过在页面预渲染阶段注册提交前的脚本,使得所有需要禁用的按钮在提交时被设为不可用状态,有效避免了因网络延迟等原因造成的重复提交问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  一:  开题

    这篇只是一个简单的应用技巧,高手请跳过,别拍砖,打击我这个有激情的菜鸟。在我们的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你可以自己定义覆盖默认的,默认为:

private System.Predicate isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||
                       (t is System.Web.UI.WebControls.LinkButton) ||
                       (t is System.Web.UI.WebControls.ImageButton) ||
                       (t is System.Web.UI.HtmlControls.HtmlButton) ||
           //(t is System.Web.UI.HtmlControls.HtmlLink) ||
                       (t is System.Web.UI.HtmlControls.HtmlInputButton) ||
                       (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部分

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


namespace Wolf.Utils
{
    public class DoubleSubmitPrevent
    {
        private System.Collections.Generic.List _clientIDList = null;
        private const string DOUBLE_SUBMIT_PREVENT_STR = "B3F6F682-F404-4519-9F30-79876E5A5C9A_WOLF_DOUBLESUBMITPREVENT_391B8D4F-757E-4005-8262-062652D8BAC6";
        private bool isAutoFind = false;
        #region judje  Prevent Control?
        private System.Predicate isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||
                        (t is System.Web.UI.WebControls.LinkButton) ||
                        (t is System.Web.UI.WebControls.ImageButton) ||
                        (t is System.Web.UI.HtmlControls.HtmlButton) ||
            //(t is System.Web.UI.HtmlControls.HtmlLink) ||
                        (t is System.Web.UI.HtmlControls.HtmlInputButton) ||
                        (t is System.Web.UI.HtmlControls.HtmlInputSubmit);
        #endregion
        private System.Web.UI.Control baseContrlForFind = null;

        ///
        /// Auto Find will satrt with this Control;Default this Page .
        ///
        public System.Web.UI.Control BaseContrlForFind
        {
            get { return baseContrlForFind; }
            set { baseContrlForFind = value; }
        }

        ///
        /// judje the Contrl that be prevented;
        ///
        public System.Predicate IsPreventControl
        {
            get { return isPreventControl; }
            set { isPreventControl = value; }
        }
        ///
        /// Auto Find the Control that be prevented ?
        ///
        public bool IsAutoFind
        {
            get { return isAutoFind; }
            set { isAutoFind = value; }
        }

        public DoubleSubmitPrevent(System.Web.UI.Page page)
        {
            _clientIDList = new System.Collections.Generic.List();
            baseContrlForFind = page;
            page.PreRender += new System.EventHandler(DoubleSubmitPreventPagePreRenderHick);
        }

        public DoubleSubmitPrevent Add(string clientID)
        {
            _clientIDList.Add(clientID);
            return this;
        }
        public DoubleSubmitPrevent Add(System.Web.UI.Control ctr)
        {
            _clientIDList.Add(ctr.ClientID);
            return this;
        }

        public DoubleSubmitPrevent AddRange(params string[] clientIDs)
        {
            _clientIDList.AddRange(clientIDs);
            return this;
        }

        public DoubleSubmitPrevent AddRange(params System.Web.UI.Control[] ctrs)
        {
            foreach (var item in ctrs)
            {
                Add(item);
            }
            return this;
        }

        public DoubleSubmitPrevent Remove(string clientID)
        {
            _clientIDList.Remove(clientID); return this;
        }

        public DoubleSubmitPrevent Remove(System.Web.UI.Control ctr)
        {
            _clientIDList.Remove(ctr.ClientID);
            return this;
        }

        public bool Exists(string clientID)
        {
            return _clientIDList.Exists(t => t.Equals(clientID));
        }

        public bool Exists(System.Web.UI.Control ctr)
        {
            return _clientIDList.Exists(t => t.Equals(ctr.ClientID));
        }

        protected virtual void DoubleSubmitPreventPagePreRenderHick(object sender, System.EventArgs e)
        {
            System.Web.UI.Page page = sender as System.Web.UI.Page;
            if (page != null)
            {

                if (isAutoFind)
                {
                    #region Find Action
                    System.Action, System.Web.UI.Control> action = null;
                    action = (list, ctr) =>
                    {
                        if (ctr != null)
                        {
                            if (isPreventControl(ctr))
                            {
                                list.Add(ctr.ClientID);
                            }
                            foreach (System.Web.UI.Control item in ctr.Controls)
                            {
                                action(list, item);
                            }
                        }

                    };
                    #endregion
                    action(_clientIDList, baseContrlForFind);

                }

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (var item in _clientIDList)
                {
                    sb.Append(string.Format(" document.getElementById(/"{0}/").disabled=true;", item));
                }
                page.ClientScript.RegisterOnSubmitStatement(this.GetType(), DOUBLE_SUBMIT_PREVENT_STR, sb.ToString());
            }
        }
    }
}

 

  三: 测试:

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

protected void Page_Load(object sender, EventArgs e)
   {
       if (IsPostBack)
       {
           System.Threading.Thread.Sleep(1000 * 5);
           TextBox1.Text = DateTime.Now.ToString();
           Button3.Enabled = false;
       }
       Wolf.Utils.DoubleSubmitPrevent dsp = new Wolf.Utils.DoubleSubmitPrevent(this) { IsAutoFind = true, BaseContrlForFind = this.form1 };
       //dsp.Add(Button1).Add(Button2).Add(Button3).Add(LinkButton1).Add(LinkButton2);
      
   }
前台html(乱托乱扔的,看来是比较懒8{U`QQB5X27@C_FO](KQ(4G ):
<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
     <asp:Button ID="Button2" runat="server" Text="Button" />
      <asp:Button ID="Button3" runat="server" Text="Button" />
    <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><asp:LinkButton
        ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>
    </form>   

效果:

F]KRP2ZDF95F1FSZ)L57(NN

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值