最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple submit Button click problem? - Stack Overflow

programmeradmin5浏览0评论

I have a form which inserts data in DB on Submit button click but the problem is when client click the button multiple times its sends multiple create requests means multiple button click events for the same time of same data, which must not be.

I tried to disable the button when client click the Submit button first time but after this it does not call server click event handler or not fire the server click event once it got disabled.

How to handle this multiple click problem..

I used the following code to disable the button

 <script type="text/javascript">
     function checkAuth(obj)
     {
         if(Page_ClientValidate("ValidationGroupName"))
            obj.disabled=true;      
     }
 </script>

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
OnClick="btnSubmit_click" OnClientClick="checkAuth(this)" CssClass="FormButton" 
ValidationGroup="ValidationGroupName" />

I have a form which inserts data in DB on Submit button click but the problem is when client click the button multiple times its sends multiple create requests means multiple button click events for the same time of same data, which must not be.

I tried to disable the button when client click the Submit button first time but after this it does not call server click event handler or not fire the server click event once it got disabled.

How to handle this multiple click problem..

I used the following code to disable the button

 <script type="text/javascript">
     function checkAuth(obj)
     {
         if(Page_ClientValidate("ValidationGroupName"))
            obj.disabled=true;      
     }
 </script>

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
OnClick="btnSubmit_click" OnClientClick="checkAuth(this)" CssClass="FormButton" 
ValidationGroup="ValidationGroupName" />
Share Improve this question edited Oct 19, 2010 at 13:50 Azhar asked Oct 19, 2010 at 10:11 AzharAzhar 20.7k38 gold badges149 silver badges211 bronze badges 2
  • 2 Duplicate: stackoverflow./questions/50426/… – Tim Schmelter Commented Oct 19, 2010 at 10:13
  • 1 With the recently-added code, this is no longer a duplicate (not that you had any way of knowing that). – jwheron Commented Oct 19, 2010 at 14:34
Add a ment  | 

3 Answers 3

Reset to default 5

Do not disable the button, just prevent the second submit.

this little script does the job but it assumes there is a postback at a certain moment.

var formhandler = function() {
   var submit, isSubmit = false;
   submit = function(){
                // flop and return false once by the use of operator order.
    return isSubmit != (isSubmit = true);
    };
    return {
       submit: submit
    };
}(); // <-- use direct invcation to keep the internal variables "static"

attach it by :

   document.forms[0].onsubmit = formhandler.submit;    

or

   OnClientClick = "formhandler.submit()";

Include a unique id in a <input type="hidden"> field. Then on the server check if the request has already been processed.

As an ID you can generate a GUID. Then you just need some structure to collect currently valid IDs. For example a Dictionary. Then from time to time you can clear old IDs out of the structure, and of course remove it when it is used.

All you have to do is have some client side javascript which disables the button. Something similar to:

document.myform.mybutton.disabled  = true;

If you wrap this in a function, and set the onClientClick attribute of your button to fire this. As soon as it gets clicked it is disabled - but still generates the request to the server.

The reason the event didnt fire the way you suggested above is likely because you disabled server side (not client side as is here)

发布评论

评论列表(0)

  1. 暂无评论