天天看点

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

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

代码

private system.predicate<system.web.ui.control> 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);包括的多个重载;

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

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

namespace wolf.utils 

    public class doublesubmitprevent 

    { 

        private system.collections.generic.list<string> _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<system.web.ui.control> 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;

        /// <summary> 

        /// auto find will satrt with this control;default this page . 

        /// </summary> 

        public system.web.ui.control basecontrlforfind 

        { 

            get { return basecontrlforfind; } 

            set { basecontrlforfind = value; } 

        }

        /// judje the contrl that be prevented; 

        public system.predicate<system.web.ui.control> 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<string>(); 

            basecontrlforfind = page; 

            page.prerendercompleted += 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); 

        public doublesubmitprevent addrange(params string[] clientids) 

            _clientidlist.addrange(clientids); 

        public doublesubmitprevent addrange(params system.web.ui.control[] ctrs) 

            foreach (var item in ctrs) 

            { 

                add(item); 

            } 

        public doublesubmitprevent remove(string clientid) 

            _clientidlist.remove(clientid); return this; 

        public doublesubmitprevent remove(system.web.ui.control ctr) 

            _clientidlist.remove(ctr.clientid); 

        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.collections.generic.list<string>, 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); 

   } 

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

<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>

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

效果:

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

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

继续阅读