单击后禁用按钮,同时保留 CausesValidation 和 OnClick-Method

发布于 2024-12-21 12:29:30 字数 748 浏览 4 评论 0原文

所以我有这个按钮:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2024-12-28 12:29:30

只需覆盖您的页面或母版页的 Onload 事件即可

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;");
}

Just override the Onload event of ur page or master page with

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;");
}
兮子 2024-12-28 12:29:30

中尝试以下操作

在 Page_Load VB

Dim postBackOptions As PostBackOptions = New PostBackOptions(btnSubmit)
btnSubmit.OnClientClick = "this.disabled=true;"
btnSubmit.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptionClaim)

C#

PostBackOptions postBackOptions = new PostBackOptions(btnSubmit);
btnSubmit.OnClientClick = "this.disabled=true;";
btnSubmit.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptionClaim);

Try the following in Page_Load

VB

Dim postBackOptions As PostBackOptions = New PostBackOptions(btnSubmit)
btnSubmit.OnClientClick = "this.disabled=true;"
btnSubmit.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptionClaim)

C#

PostBackOptions postBackOptions = new PostBackOptions(btnSubmit);
btnSubmit.OnClientClick = "this.disabled=true;";
btnSubmit.OnClientClick += ClientScript.GetPostBackEventReference(postBackOptionClaim);
泼猴你往哪里跑 2024-12-28 12:29:30

编辑

if(Page_ClientValidate()) {this.visibility='hidden';}

如果您需要在知道是否隐藏/禁用按钮之前进行服务器端验证,您可能会想要放弃禁用按钮,而只需确保您的服务器端代码不这样做如果用户敲击按钮,则不会执行不必​​要的操作。

您可以在 page_load if(!IsPostBack) 中放置一个隐藏字段并为其生成 GUID,然后在 btnSubmit_click 上执行类似的操作

if(Session[Page.ToString() + "_spam"] != null && Session[Page.ToString() + "_spam"] == hdnGuid.Value) { 
    return 
} else { 
    Session[Page.ToString() + "_spam"] = hdnGuid.Value; 
    //do stuff 
}

EDIT

if(Page_ClientValidate()) {this.visibility='hidden';}

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

if(Session[Page.ToString() + "_spam"] != null && Session[Page.ToString() + "_spam"] == hdnGuid.Value) { 
    return 
} else { 
    Session[Page.ToString() + "_spam"] = hdnGuid.Value; 
    //do stuff 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文