简体   繁体   English

触发JQuery事件后未触发Codebehind OnClick事件

[英]Codebehind OnClick event is not fired after firing JQuery event

I'm using html5 constraint validation on a few fields on my form, such as: 我在表单上的几个字段上使用html5约束验证,例如:

<asp:TextBox ID="TextBox1" runat="server" Required="required" ToolTip="Description is required."></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

And I would like to disable the submit button through CSS and JQuery on click, but only AFTER the validation passes. 我想在单击时通过CSS和JQuery禁用提交按钮,但是只有在验证通过之后。 I attempted to hook into the form submit event like so: 我试图钩入表单提交事件,如下所示:

<script type='text/javascript'>
var $form = $('#form1');
var $submitButton = $('#<% = btnSubmit.ClientID %>');

$form.submit(function () {
$submitButton.prop('disabled', true);
})
</script>

Which successfully disables the button after validation, a postback is called, but my btnSubmit_click event is not fired. 验证后成功禁用按钮,调用回发,但未触发btnSubmit_click事件。

How do I determine through javacript/jquery when html5 constraint validation has passed so I can execute a jquery function that disables the button but then still allows my btnSubmit_click event to fire? 当html5约束验证通过时,如何通过javacript / jquery确定,以便我可以执行禁用按钮的jquery函数,但仍允许btnSubmit_click事件?

Update: I'm using plain JQuery 2.0.3 更新:我正在使用普通的JQuery 2.0.3

It's a common issue to fire Codebehind events and JQuery events simultaneously in some cases. 在某些情况下,同时触发Codebehind事件和JQuery事件是一个常见问题。 you can use some tricks to overcome this issue but i'm using this one as a dirty solution, maybe there are some cleaner ways too. 您可以使用一些技巧来解决此问题,但是我将其用作肮脏的解决方案,也许还有一些更清洁的方法。

You can let the JQuery form.submit event fire normally in first time, but when you click on Submit Button, add a "DontFireJQuery" class to button and after set button as disable, It's fired again automatically using : 您可以让JQuery form.submit事件在第一时间正常触发,但是当您单击Submit Button时,在button上添加一个“ DontFireJQuery”类,然后在将button设置为disable时,可以使用再次自动触发:

__doPostBack("<%= btnSubmit.UniqueID %>", "");

In the next firing, the button has "DontFireJQuery" class and JQuery skip it, so the Codebehind OnClick event is fired. 在下一次触发中,按钮具有“ DontFireJQuery”类,并且JQuery跳过它,因此将触发Codebehind OnClick事件。

<asp:TextBox ID="TextBox1" runat="server" Required="required" ToolTip="Description is required."></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>

<script type='text/javascript'>
    Sys.Application.add_load(initJScripts);
    function initJScripts() {
        var $form = $('#form1');
        var $submitButton = $('#<% = btnSubmit.ClientID %>');
        if (!$submitButton.hasClass("DontFireJQuery")) {
            $form.submit(function () {
                $submitButton.addClass("DontFireJQuery");
                $submitButton.prop('disabled', true);
                __doPostBack("<%= btnSubmit.UniqueID %>", "");
            })
        }
    }
</script>

In CodeBehind : 在CodeBehind中:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    System.Threading.Thread.Sleep(1000)
    lblMsg.Text = "OK"
End Sub

(to convert VB to C# you can use this online tool if needed: http://codeconverter.sharpdevelop.net/SnippetConverter.aspx ) (要将VB转换为C#,可以根据需要使用此在线工具: http : //codeconverter.sharpdevelop.net/SnippetConverter.aspx

This line: 这行:

System.Threading.Thread.Sleep(1000)

is for test, in order to show that the button is set as disable immediately and 1 sec later, "OK" message will be shown in lblMsg. 为了进行测试,为了显示该按钮立即设置为“禁用”,并在1秒钟后,在lblMsg中将显示“ OK”消息。 as you know, after each postback, the changes that applied by JQuery will be vanished and button is set as enable again when the OK message shown. 如您所知,每次回发后,JQuery所应用的更改都将消失,并且在显示OK消息时,按钮将再次设置为启用。 if you want to set button as disable permanently, you can change the code behind like this: 如果您要将按钮永久设置为禁用,则可以像下面这样更改后面的代码:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    System.Threading.Thread.Sleep(1000)
    btnSubmit.Enabled = False
    lblMsg.Text = "OK"
End Sub

This code is written in a way that can be used in UpdatePanel without any problems too. 这段代码的编写方式可以在UpdatePanel中使用,也没有任何问题。

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

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