单击后禁用按钮,同时保留 CausesValidation 和 OnClick-Method
所以我有这个按钮:
<asp:Button runat="server" ID="btnSubmit" meta:resourcekey="btnSubmit" CausesValidation="true" OnClick="btnSubmit_Click" />
meta:resourcekey 用于本地化资源,这里与我们无关 - 因为我们可以看到它有一个 OnClick-Method 并导致验证。
这也很好用,但我想在用户单击该按钮后禁用该按钮,这样他/她就无法在回发成功之前多次单击它,所以这就是我在 Page_Load
中所做的:
btnSubmit.Attributes.Add("onclick", "this.disabled=true;" +
Page.ClientScript.GetPostBackEventReference(btnSubmit, "").ToString());
onclick 我将禁用该按钮并重新添加 OnClick-Method 所需的 PostBackReference。
问题:原因验证消失了,悲伤的表情。我究竟如何在 CodeBehind 中重新添加它,或者 - 什么是完全不同的解决方案?
我的按钮必须:
a)单击后禁用自身,但在回发后启用 b) 有一个 OnClick CodeBehind 方法 c) 原因验证
谢谢,
丹尼斯
So I've got this button:
<asp:Button runat="server" ID="btnSubmit" meta:resourcekey="btnSubmit" CausesValidation="true" OnClick="btnSubmit_Click" />
meta:resourcekey is for Localization Resources, doesn't concern us here - as we can see it has got an OnClick-Method and causes Validation.
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 I'm disabling the button and re-adding the PostBackReference necessary for the OnClick-Method.
Problem: CausesValidation is gone, sadface. How exactly would I re-add that in CodeBehind or alternatively - What's an entirely different solution to this?
My Button has to:
a) disable itself after clicking, yet be enabled after the postback
b) have an OnClick CodeBehind Method
c) cause Validation
Thanks,
Dennis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需覆盖您的页面或母版页的 Onload 事件即可
Just override the Onload event of ur page or master page with
中尝试以下操作
在 Page_Load VB
C#
Try the following in Page_Load
VB
C#
编辑
如果您需要在知道是否隐藏/禁用按钮之前进行服务器端验证,您可能会想要放弃禁用按钮,而只需确保您的服务器端代码不这样做如果用户敲击按钮,则不会执行不必要的操作。
您可以在 page_load if(!IsPostBack) 中放置一个隐藏字段并为其生成 GUID,然后在 btnSubmit_click 上执行类似的操作
EDIT
If you need to have server side validation before you know whether to hide/disable the button or not you'll probably want to forgo the disabling of the button and just make sure your server-side code doesn't execute more than necessary if a user hammers on the button.
You could put a hidden field and generate a GUID for it in the page_load if(!IsPostBack) then on your btnSubmit_click do something like