禁用按钮后提交 [英] Disable button after submit

查看:193
本文介绍了禁用按钮后提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当用户提交的支付形式和code张贴的形式导致在Firefox中双岗禁用按钮。
当code被删除,不会发生此​​问题,并在比Firefox其他任何浏览器不会发生。

I'm trying to disable a button when a user submits a payment form and the code to post the form is causing a double post in firefox. This problem does not occur when the code is removed, and does not occur in any browser other than firefox.

不知道如何prevent双重张贴在这里?

Any idea how to prevent the double post here?

System.Text.StringBuilder sb = new StringBuilder();
sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
sb.Append("if (Page_ClientValidate() == false) { return false; }} ");
sb.Append("this.value = 'Please wait...';");
sb.Append("this.disabled = true;");
sb.Append(Page.GetPostBackEventReference(btnSubmit ));
sb.Append(";");
btnSubmit.Attributes.Add("onclick", sb.ToString());

这是造成这一问题的sb.Append(Page.GetPostBackEventReference(btnSubmit按钮))行

it's the sb.Append(Page.GetPostBackEventReference(btnSubmit )) line that's causing the issue

感谢

编辑:这里是按钮的C#:

Here's the c# of the button:

<asp:Button ID="cmdSubmit" runat="server" Text="Submit" />

这里的HTML搜索结果
这code职位两次(和禁用提交按钮,验证输入):

here's the html

This code posts twice (and disables the submit button and verifies input):

<input type="submit" name="ctl00$MainContent$cmdSubmit" value="Submit" onclick="if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }} this.value = 'Please wait...';this.disabled = true;document.getElementById('ctl00_MainContent_cmdBack').disabled = true;__doPostBack('ctl00$MainContent$cmdSubmit','');" id="ctl00_MainContent_cmdSubmit" />

结果这code职位的两倍(但不会禁用提交按钮):


This code posts twice (but doesn’t disable the submit button):

<input type="submit" name="ctl00$MainContent$cmdSubmit" value="Submit" onclick="__doPostBack('ctl00$MainContent$cmdSubmit','');" id="ctl00_MainContent_cmdSubmit" />

结果这code职位一次(但不验证用户输入,并不会禁用提交按钮):


This code posts once (but doesn’t verify the user input and doesn’t disable the submit button):

<input type="submit" name="ctl00$MainContent$cmdSubmit" value="Submit" id="ctl00_MainContent_cmdSubmit" />

结果这code职位一次(但不能禁用提交按钮):


This code posts once (but doesn’t disable submit button):

<input type="submit" name="ctl00$MainContent$cmdSubmit" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$cmdSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_MainContent_cmdSubmit" />

这code不张贴在所有:
搜索结果

This code doesn’t post at all:

<input type="submit" name="ctl00$MainContent$cmdSubmit" value="Submit" onclick="this.disabled = true;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$cmdSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_MainContent_cmdSubmit" />

显然,这是提交按钮的冒充问题的禁用。你有什么想法我们如何可以禁用提交,避免多次点击?

Obviously it’s the disabling of the submit button that’s posing the problem. Do you have any ideas how we can disable the submit to avoid multiple clicking?

推荐答案

presumably, btnSubmit按钮已经勾搭上了一个服务器端事件。如果是的话,调用 Page.GetPostBackEventReference 不应该是必要的。你应该删除该行得到你想要的行为根本。

Presumably, btnSubmit already has a server-side event hooked up. If so, the call to Page.GetPostBackEventReference should not be necessary. You should get your desired behavior simply by removing that line.

更新:您提到安装在C#code中的事件处理程序,但你不提,你这样做。我猜这是在的Page_Load 处理程序。如果是这样的话,它不会正常工作,因为它是为时已晚在这一点上挂接一个按钮单击事件处理程序。让我们来试试这个吧。

Update: You mentioned attaching the event handler in C# code, but you don't mention where you do that. I'm guessing it's in the Page_Load handler. If that is the case, it wouldn't work properly, as it's too late to hook up a button click event handler at that point. Let's try this instead.

首先,这将是更清洁把JS到它自己的功能,而不是在C#code-背后构建它。我建议你​​把它变成一个脚本块(或更好,但它自己的.js文件。)

First, it would be cleaner to put the JS into it's own function rather than building it in the C# code-behind. I suggest that you put it into a script block (or better yet, it's own .js file.)

function disableOnSubmit(target)
{
    if (typeof(Page_ClientValidate) == 'function') {
        if (Page_ClientValidate() == false) { return false; }
    }
    target.value = 'Please wait...';
    target.disabled = true;
    return true;
}

和你的ASPX按钮,试试这个:

And for your ASPX button, try this:

<asp:Button ID="cmdSubmit" runat="server" Text="Submit" onclick="btnSumbit_Click" OnClientClick="return disableOnSubmit(this);" />

这篇关于禁用按钮后提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆