Codebehind Onclick Event Is Not Fired After Firing Jquery Event
Solution 1:
It's a common issue to fire Codebehind events and JQuery events simultaneously in some cases. 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 :
__doPostBack("<%= btnSubmit.UniqueID %>", "");
In the next firing, the button has "DontFireJQuery" class and JQuery skip it, so the Codebehind OnClick event is fired.
<asp:TextBoxID="TextBox1"runat="server"Required="required"ToolTip="Description is required."></asp:TextBox><br /><asp:ButtonID="btnSubmit"runat="server"Text="Submit" /><br /><asp:LabelID="lblMsg"runat="server"Text=""></asp:Label><scripttype='text/javascript'>Sys.Application.add_load(initJScripts);
functioninitJScripts() {
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 :
PrivateSub btnSubmit_Click(sender AsObject, e As EventArgs) Handles btnSubmit.Click
System.Threading.Thread.Sleep(1000)
lblMsg.Text = "OK"EndSub
(to convert VB to C# you can use this online tool if needed: 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. 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. if you want to set button as disable permanently, you can change the code behind like this:
PrivateSub btnSubmit_Click(sender AsObject, e As EventArgs) Handles btnSubmit.Click
System.Threading.Thread.Sleep(1000)
btnSubmit.Enabled = False
lblMsg.Text = "OK"EndSub
This code is written in a way that can be used in UpdatePanel without any problems too.
Post a Comment for "Codebehind Onclick Event Is Not Fired After Firing Jquery Event"