简体   繁体   English

多次提交按钮单击问题?

[英]Multiple submit Button click problem?

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

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. <input type="hidden">字段中包含唯一ID。 Then on the server check if the request has already been processed. 然后在服务器上检查请求是否已被处理。

As an ID you can generate a GUID. 作为ID,您可以生成一个GUID。 Then you just need some structure to collect currently valid IDs. 然后,您只需要一些结构即可收集当前有效的ID。 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. 然后,您可以不时地从结构中清除旧ID,当然也可以在使用它时将其删除。

All you have to do is have some client side javascript which disables the button. 您所要做的就是安装一些禁用该按钮的客户端javascript。 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. 如果将其包装在函数中,然后设置按钮的onClientClick属性以触发此操作。 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) 事件未按照您上面建议的方式触发的原因可能是因为您禁用了服务器端(而不是此处的客户端)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM