Asp.net

__doPostBack() 之後的回調?

  • January 7, 2016

我通過呼叫這樣的方法用 Javscript 刷新 UpdatePanel:

reloadDropDown = function (newValue)
{
 __doPostBack("DropDown1", "");
 selectNewValueInDropDown(newValue);
}

在我的 UpdatePanel 中有一個<select>框,我需要選擇一個<option>帶有newValue的框。我的問題是我的selectNewValueInDropDown方法在完成之前被呼叫__doPostBack。有沒有辦法在呼叫我的selectNewValueInDropDown方法之前“等待”回發?

為了使我的評論更具體,這裡的想法是:

reloadDropDown = function (newValue)
{
   var requestManager = Sys.WebForms.PageRequestManager.getInstance();

   function EndRequestHandler(sender, args) {
       // Here's where you get to run your code!
       selectNewValueInDropDown(newValue);

       requestManager.remove_endRequest(EndRequestHandler);
   }
   requestManager.add_endRequest(EndRequestHandler);

   __doPostBack("DropDown1", "");
}

當然,您可能想要處理兩個請求重疊的競爭條件。為了處理這個問題,您需要跟踪哪個處理程序針對哪個請求。您可以在伺服器端使用類似的東西ScriptManager.RegisterDataItem,或致電args.get_panelsUpdated()並檢查您感興趣的面板是否已更新。

引用自:https://stackoverflow.com/questions/7615691