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