2013-01-31 145 views
0

在計算複雜的公式服務器端後,我必須顯示客戶端確認。喜歡的東西僅在客戶端確認後執行服務器端代碼

//Server side 
     On ButtonClick(){ 
      FetchRate(field1,field2,.... fieldn); 
      // Show Client side confirmation 
      // Execute server side code if confirmed client side 
    } 

我所做的是創建一個客戶端的功能,但我的服務器端代碼總是在回發執行不管我選擇客戶端

// Server side  
ScriptManager.RegisterStartupScript(this,this.GetType(), Guid.NewGuid().ToString(), "ConfirmAction('"+ myRate +"');", true); 
//Client side 
    function ConfirmAction(myRate) { 
      if (confirm('Are you sure?. Rate is exceeding '+ myRate +', proceed ?')) { 
       document.getElementById('hfSaveUpdate').value = 1; 
       return true; 
      } 
      else 
       return false; 
     } 

回答

2

您可以使用Ajax Model Popup &句柄ok &取消按鈕。

<ajaxToolkit:ModalPopupExtender ID="ModelPopupID" runat="server" 
    TargetControlID="LinkButton1" 
    PopupControlID="Panel1" 
    BackgroundCssClass="modalBackground" 
    DropShadow="true" 
    OkControlID="OkButton" 
    OnOkScript="onOk()" 
    CancelControlID="CancelButton" 
    PopupDragHandleControlID="Panel3" /> 

服務器代碼啓動一個模式彈出窗口:

服務器端代碼:

ClientScript.RegisterStartupScript(this.GetType(), "key", "launchModal();", true); 

客戶端代碼:

<script type="text/javascript"> 
var launch = false; 
function launchModal() 
{ 
launch = true; 
} 
function pageLoad() 
{ 
if (launch) 
{ 
$find("ModelPopupID").show(); 
} 
} 
</script> 

在模型彈出的OK點擊,在確認代碼後執行服務器端。 點擊取消按鈕,只需隱藏模型彈出窗口。

有關詳細信息,檢查:

ModalPopup Tutorial

2

您不能在服務器端代碼之間放置客戶端操作。

您的代碼準備客戶端確認,但不會將其發送到瀏覽器,直到響應完成。服務器立即繼續處理您的數據。完成後,響應會發送到瀏覽器,用戶將看到確認對話框。太晚了:數據已經被處理了。對話結果永遠不會被髮送到服務器。

您需要將流程拆分爲兩部分:首先獲得有關該費率的確認(可能使用ajax),然後提交要處理/存儲的表單。

相關問題