I needed to call a function before postback in Asp.Net. Is there anyway to do this using JQuery?
-
Please also post code from which control you want to call the function and what the function will do on call?Suprabhat Biswal– Suprabhat Biswal2015-10-14 05:13:38 +00:00Commented Oct 14, 2015 at 5:13
-
@Tushar it's not workingNishantha– Nishantha2015-10-14 05:19:51 +00:00Commented Oct 14, 2015 at 5:19
-
@Suprabhat assume it's a simple javascript functionNishantha– Nishantha2015-10-14 05:20:25 +00:00Commented Oct 14, 2015 at 5:20
-
Do tou want to perform logic when a user clicks a button and before the postback?Anders Stensaas– Anders Stensaas2015-10-14 05:20:33 +00:00Commented Oct 14, 2015 at 5:20
-
@Saasen No. Actually I want to know is there any way to do this without specific element postback, it will call any postback event.Nishantha– Nishantha2015-10-14 05:24:30 +00:00Commented Oct 14, 2015 at 5:24
Add a comment
|
3 Answers
You can use following jQuery script in your code to call your function in the beginRequest. You must set the AutoPostBack property of those controls to true for which you want to initiate this before callback function call
<script type="text/javascript">
jQuery(function ($) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(function (source, args) {
// YOUR FUNCTION CALL HERE such as display processing/loading prompt/div
});
prm.add_endRequest(function (source, args) {
// any other code you want to execute after the post back finishes such as hiding processing/loading prompt/div
});
});
</script>
Be sure to add a ScriptManager and UpdatePanel to your page. For more details you may check this MSDN reference
1 Comment
Psddp
It seems the event is not triggered for AutoPostBack, is there another event for that?
Example of calling a jquery function on server side with Page_Load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'"Expandir Compras" its the Unique identifier of the script code
ScriptManager.RegisterStartupScript(Me, [GetType](), "Expandir Compras", "$('#collapseCompras').collapse('show');", True)
End Sub
Maybe you can do something similar with the right event (Save Changes, Pre Load, Data Binding, etc). Hope it helps!
Comments
You can use the following code
$(document).ready(function() {
$("form").submit(function() {
// do the extra stuff here
//return true to submit the form
//return false if do not want to submit form
});
});
1 Comment
haraman
Don't you think that
$("form").submit(function() will run only if we click the submit button on the form and not on any other button