简体   繁体   English

防止在ASP.NET中提交多个表单

[英]Preventing multiple form submits in ASP.NET

I'm currently searching for a way to prevent multiple form submits in ASP.NET (ie the user clicks on a submitbutton several times, causing the OnClick-Event to fire several times also). 我目前正在寻找一种方法来阻止ASP.NET中的多个表单提交(即用户多次点击提交按钮,导致OnClick-Event也会多次触发)。

Currently, I'm doing it like this: 目前,我这样做:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Page.ClientScript.RegisterOnSubmitStatement(GetType(), "ServerForm",
        "if (this.submitted) return false; this.submitted = true; return true;");
    }

This works like a charm for synchronous postbacks - the 'submitted'-value will be reset after each postback and prevent the form from being submitted multiple times in the same run - however, this method does not work for AsyncPostbacks ie with an UpdatePanel, so now I'm searching for an idea to make this happen without having to dis- and enable each and every button via JS after clicking a button. 这类似于同步回发的魅力 - 每次回发后都会重置“提交”值并阻止表单在同一次运行中多次提交 - 但是,此方法不适用于AsyncPostbacks,即使用UpdatePanel,因此现在我正在寻找一个想法来实现这一点,而无需在单击按钮后通过JS禁用和启用每个按钮。

Thanks, 谢谢,

Dennis 丹尼斯

You can use Page.get_isInAsyncPostBack() in javascript to check the state of async request. 您可以在javascript中使用Page.get_isInAsyncPostBack()来检查异步请求的状态。

Refer this link for more information: http://ranaictiu-technicalblog.blogspot.com/2009/03/prevent-concurrent-asynchronous.html 有关更多信息,请参阅此链接: http//ranaictiu-technicalblog.blogspot.com/2009/03/prevent-concurrent-asynchronous.html

why not you check in your database ? 你为什么不登记你的数据库? if the user already registerd then give a alert box. 如果用户已经注册,则给出一个警告框。 and you can also check PRG pattern 你也可以检查PRG模式

Handling Form submission with PRG 使用PRG处理表单提交

  1. You can check the start & end methods present in class Sys.WebForms.PageRequestManager to check for ASync requests. 您可以检查类Sys.WebForms.PageRequestManager的开始和结束方法以检查Sys.WebForms.PageRequestManager请求。
  2. Sys.WebForms.PageRequestManager You can modify the code in above example & disable the button by using simple javascript like Sys.WebForms.PageRequestManager您可以修改上面示例中的代码并使用简单的javascript禁用该按钮

    document.getElementById("YouButton.ClientID").disabled = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM