单击后禁用按钮,同时保持CausesValidation和OnClick-Method [英] Disable Button after Click whilst maintaining CausesValidation and an OnClick-Method
问题描述
所以我有这个按钮:
<asp:Button runat="server" ID="btnSubmit" meta:resourcekey="btnSubmit" CausesValidation="true" OnClick="btnSubmit_Click" />
meta:resourcekey用于本地化资源,在这里与我们无关-我们可以看到它具有一个OnClick方法并引起了验证.
meta:resourcekey is for Localization Resources, doesn't concern us here - as we can see it has got an OnClick-Method and causes Validation.
这也很好,但是我想在用户单击按钮后禁用该按钮,这样他/她就无法在PostBack成功之前多次单击它,所以这就是我在Page_Load
中所做的事情:
That works fine, too, but I'd like to disable that button after the user clicked it so he/she can't click it multiple times before the PostBack succeeds, so here's what I did in Page_Load
:
btnSubmit.Attributes.Add("onclick", "this.disabled=true;" +
Page.ClientScript.GetPostBackEventReference(btnSubmit, "").ToString());
onclick我禁用了按钮,并重新添加了OnClick-Method所需的PostBackReference.
onclick I'm disabling the button and re-adding the PostBackReference necessary for the OnClick-Method.
问题:CausesValidation消失了,悲伤的表情.我将如何精确地在CodeBehind或替换项中重新添加它?对此有什么完全不同的解决方案?
Problem: CausesValidation is gone, sadface. How exactly would I re-add that in CodeBehind or alternatively - What's an entirely different solution to this?
我的按钮必须:
a)单击后禁用自身,但在回发后启用 b)具有一个OnClick CodeBehind方法 c)原因验证
a) disable itself after clicking, yet be enabled after the postback b) have an OnClick CodeBehind Method c) cause Validation
谢谢
丹尼斯
推荐答案
只需使用
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// prevents form from being submitted multiple times in MOST cases
// programatic client-side calls to __doPostBack() can bypass this
Page.ClientScript.RegisterOnSubmitStatement(GetType(), "ServerForm",
"if (this.submitted) return false; this.submitted = true; return true;");
}
这篇关于单击后禁用按钮,同时保持CausesValidation和OnClick-Method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!