防止二次提交 UseSubmitBehavior属性 asp.net

//方法一:必需要是Asp.net服务器控件。(注意UseSubmitBehavior属性)
<asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false"
OnClientClick="this.value=‘正在提交‘;this.disabled=true; "
Text="提交" OnClick="btnSumbit_Click" />

注意:1. UseSubmitBehavior="false"  需要设置成false

        2. Button不能有ValidationGroup属性,否则,postback到服务器端,将不会触发button的服务器端事件


<script> 
 function DisableButton(b) { 
   b.disabled = true; 
   b.value = ‘Submitting‘; 
 } 
</script>


<asp:Button ID="Button1" runat="server" UseSubmitBehavior="false"
OnClick="Button1_Click"  Text="Button" OnClientClick="DisableButton(this)" />

OnClientClick的属性来调用Javascript从而实现Disable按钮。 UseSubmitBehavior属性设为false表示不用客户端方法提交,从服务器端提交,也就是执行OnClick的事件。反之,如果是true,则从客户端提交,忽略服务器端的事件

原链:http://www.cnblogs.com/nixjojo/archive/2012/04/20/2459436.html


//方法二

当遇上Validator控件

 <script type="text/javascript">
     function disableButton(button)
     {
         if (typeof (Page_ClientValidate) == ‘function‘ && Page_ClientValidate() == false)
         {
             return false;
         }
         button.disabled = true;
         return true;
     }
 </script>
 
 <div>
     <asp:TextBox runat="server" ID="tbxInput1"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbxInput1"
             ValidationGroup="Group1" ErrorMessage="*"></asp:RequiredFieldValidator>
     <asp:Button runat="server" ID="btnPostBack1" Text="按我1" UseSubmitBehavior="false"
             OnClientClick="disableButton(this);" OnClick="btnPostBack_Click">
         </asp:Button>
 </div>


//方法三 
  protected void Page_Load(object sender, EventArgs e)

    {
            //.net1.1
            Button1.Attributes.Add("onclick", "this.disabled=true;" + this.GetPostBackEventReference(this.Button1));

            //.net 2.0以上
        Button1.Attributes.Add("onclick", "this.disabled=true;" + this.ClientScript.GetPostBackEventReference(Button1, ""));

    }

 

//终极绝杀技

 <script type="text/javascript">
         var date = new Array();
         function disableButton(button, validateGroup)
         {
             date.push(new Date());
             if (date.length > 1
                 && (date[date.length - 1].getTime() - date[date.length - 2].getTime() < 1000))//小于1秒则认为重复提交
             {
                 event.cancelBubble = true; //测试时发现,如果直接单独设置cancelBubble、或者returnValue来取消事件,经常取消不了,依然存在重复提交的可能。因此只好用返回值来实现手工控制!
                 return false;
             }
             if (typeof (Page_ClientValidate) == ‘function‘
                 && ((validateGroup == undefined && Page_ClientValidate() == false)
                         || (validateGroup != undefined && Page_ClientValidate(validateGroup) == false)))
             { //如果指定了ValidateGroup,则只验证改组;如果未指定,则验证全部验证控件
                 return false;
             }
 
             button.disabled = true;
             return true;
         }
 </script>

 

<asp:Button runat="server" ID="btnPostBack1" Text="按我1" UseSubmitBehavior="false"
           ValidationGroup="Group1" OnClientClick="if(!disableButton(this, ‘Group1‘)) return false;" OnClick="btnPostBack_Click">

 <asp:Button runat="server" ID="btnPostBack2" Text="按我2" UseSubmitBehavior="false"
          OnClientClick="if(!disableButton(this)) return false;" OnClick="btnPostBack_Click">

PS:disableButton接收一个validateGroup参数,如果指定了ValidateGroup,则只验证改组;如果未指定,则验证全部验证控件

 

 

 

/// 图片按钮ImageButton防止多次提交

由于ImageButton没有UseSubmitBehavior的属性,所以

  function Submit(source) {
            Page_ClientValidate();
            if (Page_IsValid) {
                source.disabled = true;
                __doPostBack(source.name, ‘‘);
            }
            return Page_IsValid;
        }

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Source/images/btsend.jpg"
                        OnClick="ImageButton1_Click" ToolTip="送出" OnClientClick="javascript:Submit(this);" />



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。