1

I needed to call a function before postback in Asp.Net. Is there anyway to do this using JQuery?

5
  • Please also post code from which control you want to call the function and what the function will do on call? Commented Oct 14, 2015 at 5:13
  • @Tushar it's not working Commented Oct 14, 2015 at 5:19
  • @Suprabhat assume it's a simple javascript function Commented Oct 14, 2015 at 5:20
  • Do tou want to perform logic when a user clicks a button and before the postback? Commented 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. Commented Oct 14, 2015 at 5:24

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

It seems the event is not triggered for AutoPostBack, is there another event for that?
0

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

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.