2013-06-04 24 views
1

我有編碼爲這樣的按鈕:ASP.NET:如何提交,回傳後,才客戶端AJAX完成

<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" 
       ClientIDMode="Static" OnClientClick="a= saveAllFilt(); return a; " 
       OnClick="save_all_filt_Click" /> 

saveAllFilt是調用常用的JQuery Ajax調用的函數。事情是這樣的:

function saveAllFilt() { 
    jQuery.ajax({ 
    async: false, 
    dataType: 'json', 
    contentType: "application/json; charset=utf-8", 
    type: "POST", 
    url: contentURL, 
    data: dataarray, 
    success: function (msg) { 
     // Some processing here. ONLY WHEN IT IS DONE, I NEED TO HAVE POSTBACK. 
    }, 
    error: function (data) { 

    } 
});   // end of ajax call. 

}

我想這樣一件事:在的onClick應該僅進行後Ajax調用完成。我該怎麼做?

回答

7

您可以從按鈕移除OnClick屬性,在OnClientClick屬性一樣返回false:

<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" 
      ClientIDMode="Static" OnClientClick="saveAllFilt(); return false;" /> 

,並設置一個隱藏的按鈕與OnClick屬性,如:

<asp:Button ID="hdnButton" runat="server" Text="" Visible="False" 
      ClientIDMode="Static" OnClick="save_all_filt_Click" /> 

,並在您的ajax通話點擊該按鈕的成功方法如:

success: function (msg) { 
    // Some processing here. ONLY WHEN IT IS DONE, I NEED TO HAVE POSTBACK. 
    $('#hdnButton').click(); 
}, 
-1

在函數調用,

OnClientClick="return saveAllFilt()"; 

在阿賈克斯,

迴歸真實的,如果Ajax調用成功,併爲每一個其他情況下返回false。

-1

.ajax()調用的success屬性中的代碼後嘗試return true。優先級總是這樣:

  • 客戶端 - 側
  • 服務器端

只有當客戶端返回true服務器點擊將啓動。

function saveAllFilt() { 
    jQuery.ajax({ 
    async: false, 
    dataType: 'json', 
    contentType: "application/json; charset=utf-8", 
    type: "POST", 
    url: contentURL, 
    data: dataarray, 
    success: function (msg) { 
     // Some processing here. ONLY WHEN IT IS DONE, I NEED TO HAVE POSTBACK. 
     return true; 
    }, 
    error: function (data) { 
     return false; 
    } 
}); 
} 

而且,除去onclick屬性 - HTML中的非高性能屬性(在我看來),並使用jQuery的on()

0

這很簡單。在成功功能中調用您的回發方法。 Chekout以下代碼。

改變你的標記

<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" 
ClientIDMode="Static" OnClientClick="a=saveAllFilt(); return false;" 
OnClick="save_all_filt_Click" /> 

修改Java腳本代碼

function saveAllFilt() { 
    jQuery.ajax({ 
    async: false, 
    dataType: 'json', 
    contentType: "application/json; charset=utf-8", 
    type: "POST", 
    url: contentURL, 
    data: dataarray, 
    success: function (msg) { 
     // Some processing here.Now do the post postback 
     <%=Page.ClientScript.GetPostBackEventReference(save_all_filt,"") %> 

    }, 
    error: function (data) { 

    } 
}); 

你的想法似乎很奇怪,me.Why您試圖回傳之前調用AJAX。

0

你離得很遠。

相反的:

<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" ClientIDMode="Static" OnClientClick="a= saveAllFilt(); return a; " OnClick="save_all_filt_Click" />

嘗試:

<asp:Button ID="save_all_filt" runat="server" Text="All data for filtered subjects" ClientIDMode="Static" OnClientClick="return saveAllFilt()" OnClick="save_all_filt_Click" />

不同的是,不是OnClientClick="a= saveAllFilt(); return a;"只是做OnClientClick="return saveAllFilt()"

0

如果回真沒有了ajax內工作成功功能。在ajax成功函數中使用__postback函數。

這將僅在ajax調用完成後才提交回發。

+0

請你能解釋這是如何解決他的問題? – Gar

+0

只需在javascript中搜索_postback函數的Google語法。只需在jQuery ajax函數的成功塊中調用這個_postback函數即可。 – chandan