防止ASP.NET按钮多次提交的办法

今天在做项目时,碰到了一个问题,当页面在提交时,若网速太慢,用户可能点击多次提交按钮。这样,导致向数据库中插入了多条相同的记录。在网上搜索了一下,终于得一良方,现在拿出来与大家分享。

//方法一:在提交时调用一段客户端的代码。
function a()
{
    document.getElementById(
"btnok").value = '正在提交';
    document.getElementById(
"btnok").onclick=function(){return false;};
    
return true;
}
<input id="btnok" runat="server" type="submit" value="确定" onclick="return a();" 
onserverclick
="Submit1_ServerClick" />


//方法二:必需要是Asp.net服务器控件。(注意UseSubmitBehavior属性)
<asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false" 
OnClientClick
="this.value='正在提交';this.disabled=true; " 
Text
="提交" OnClick="btnSumbit_Click" /> 
原文地址:https://www.cnblogs.com/linyechengwei/p/1597812.html