繁体   English   中英

使用JQuery在Asp.Net中的页面回发之前调用函数

[英]Call function before page postback in Asp.Net using JQuery

我需要在Asp.Net中回发之前调用一个函数。 反正有使用JQuery做到这一点吗?

您可以在代码中使用以下jQuery脚本在beginRequest中调用函数。 您必须在回调函数调用之前将这些控件的AutoPostBack属性设置为true,要为其启动此控件

<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>

确保将ScriptManager和UpdatePanel添加到您的页面。 有关更多详细信息,请检查此MSDN参考。

您可以使用以下代码

$(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
    });
});

在服务器端使用Page_Load事件调用jquery函数的示例:

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

也许您可以对正确的事件执行类似的操作(保存更改,预加载,数据绑定等)。 希望能帮助到你!

暂无
暂无

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

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