仅在客户端确认后执行服务器端代码
问题描述:
在计算复杂的公式服务器端后,我必须显示客户端确认。喜欢的东西仅在客户端确认后执行服务器端代码
//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;
}
答
您可以使用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点击,在确认代码后执行服务器端。 点击取消按钮,只需隐藏模型弹出窗口。
有关详细信息,检查:
答
您可以在您的按钮
设置onclientclick attribute
链接:http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.button.onclientclick(v=vs.80).aspx
答
您不能在服务器端代码之间放置客户端操作。
您的代码准备客户端确认,但不会将其发送到浏览器,直到响应完成。服务器立即继续处理您的数据。完成后,响应会发送到浏览器,用户将看到确认对话框。太晚了:数据已经被处理了。对话结果永远不会被发送到服务器。
您需要将流程拆分为两部分:首先获得有关该费率的确认(可能使用ajax),然后提交要处理/存储的表单。