我知道这个问题已经被问过了,但是没有一个答案适用于我的项目。
我有一个按钮,当点击它时会调用一个API,因此会有1秒的延迟。
我尝试了几种方法但都没有成功。
btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");
即使如此也毫无作用。
我知道这个问题已经被问过了,但是没有一个答案适用于我的项目。
我有一个按钮,当点击它时会调用一个API,因此会有1秒的延迟。
我尝试了几种方法但都没有成功。
btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");
即使如此也毫无作用。
防止双击。请在您的aspx页面中添加以下代码。
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) { var oControl = args.get_postBackElement(); oControl.disabled = true; }
</script>
btnSave
的? - nu everest这个解决方案简单又有效。在您的按钮中包含以下代码:
OnClientClick="return CheckDouble();"
无论您将JavaScript放在哪里,例如在页面底部:
<script type="text/javascript">
var submit = 0;
function CheckDouble() {
if (++submit > 1) {
alert('This sometimes takes a few seconds - please be patient.');
return false;
}
}
</script>
Me.btnSave.Attributes.Add("onclick", "this.disabled=true;")
Me.btnSave.UseSubmitBehavior = False
<asp:Button ID="btnSave" runat="server" Text="Save"
UseSubmitBehavior="false"
OnClientClick="this.disabled='true';"
</asp:button>
UseSubmitBehavior="false" is the key.
您可以使用以下代码来防止双击:
Me.btnSave.Attributes.Add("onclick", "this.disabled=true;")
Me.btnSave.UseSubmitBehavior = False
你可以使用btnSave_Click
来调用你的API。
通常情况下,我在我的Page
中有很多Validators
:通过设置Validator.SetFocusOnError = True
,如果验证失败,我可以运行此代码以重新启用保存按钮。
Me.YourControl.Attributes.Add("onfocus", Me.btnSave.ClientID & ".removeAttribute('disabled');")
UseSubmitBehavior="false"
。这样做对我来说可以达到相同的效果,而不需要依赖于代码后台。 - Jacob Stamm这是我发现的在所有情况下都可行的方案。
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Clicked" />
<asp:Button ID="Button2" runat="server" Text="Button2" />
</form>
Now here’s the short JavaScript snippet that will disable the button as soon as it is clicked so that when PostBack occurs the button cannot be clicked again.
<script type = "text/javascript">
function DisableButton() {
document.getElementById("<%=Button1.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;
</script>
The above script disables the ASP.Net Button as soon as the page is ready to do a PostBack or the ASP.Net form is submitted.
But in cases you might want to disable all Buttons and Submit Buttons on the page hence for such cases I have created another function which disables all Buttons and Submit buttons whenever there’s a PostBack or form submission
<script type = "text/javascript">
function DisableButtons() {
var inputs = document.getElementsByTagName("INPUT");
for (var i in inputs) {
if (inputs[i].type == "button" || inputs[i].type == "submit") {
inputs[i].disabled = true;
}
}
}
window.onbeforeunload = DisableButtons;
</script>
防止双击。请在您的aspx页面中添加以下代码
<script type = "text/javascript">
function DisableButton() {
document.getElementById("<%=Button1.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;
</script>
起初我的解决方案是这样的:
<script>
function disableButton(btn) {
setTimeout(function () { btn.disabled = true; }, 20);
return true;
}
</script>
<asp:Button runat="server" ID="btnSave" Text="Save" OnClick="btnSave_Click" OnClientClick="return disableButton(this);" />
如果没有setTimeout,按钮将立即被禁用,然后OnClick事件将不会触发。这种方法的缺点是,如果某些验证失败或出现错误,保存按钮将不再可访问。
因此,我认为禁用按钮不是一个好的解决方案,并想出了另一个解决方案:
function disableButton(btn) {
if (btn.hasclicked) return false;
btn.hasclicked = 1;
btn.onmouseenter = function () { this.hasclicked = 0; };
return true;
}
但是我的同事指出,如果后处理非常缓慢,在完成之前,用户仍然可以通过离开-进入-点击按钮来执行双重提交。因此,我想出了另外两种解决方案:
function disableButton(btn) {
if (Page_ClientValidate) {
Page_ClientValidate();
if (!Page_IsValid) {
btn.setAttribute("btnClicked", "n");
return true;
}
}
if (btn.getAttribute("btnClicked") == "y") {
return false;
} else {
btn.setAttribute("btnClicked", "y");
return true;
}
}
由于ASP.NET页面中只有一个表单(而不是ASP.NET MVC),我们也可以让表单的onsubmit客户端事件拦截双击:
function disableButton(btn) {
$("form").submit(function () {
if (btn.getAttribute("btnClicked") == "y")
return false;
else
btn.setAttribute("btnClicked", "y");
return true;
});}
我会要求QA测试这两种方法(后编辑:QA已经证明使用此方法非常危险。请参考我的以下评论了解详情)。
这对我也起作用:
BtnID.Attributes.Add("onclick", "if(typeof (Page_ClientValidate) === 'function' && !Page_ClientValidate()){return false;} this.disabled = true;this.value = '正在处理...';" + ClientScript.GetPostBackEventReference(BtnID, null) + ";");
尝试这种方式,它是一个可行的解决方案:
对于所有浏览器,包括不支持js的Opera Mobile浏览器,这意味着您的表单在这种类型的浏览器中不会被阻止。
在Page_load()方法中添加以下内容:
BtnID.Attributes.Add("onclick", "if(typeof (Page_ClientValidate) === 'function' && !Page_ClientValidate()){return false;} this.disabled = true;this.value = 'Working...';" + ClientScript.GetPostBackEventReference(BtnID, null) + ";");