[英]Asp.net JQuery Plugin Confirm Box not working
I want to display a confirm dialog box on my asp.net website before the user upload the data. 我想在用户上传数据之前在asp.net网站上显示一个确认对话框。
I am using http://marcosesperon.es/apps/messi/ plugin for dialog box. 我正在使用http://marcosesperon.es/apps/messi/对话框插件。
The dialog box appears on client click. 客户端单击该对话框。
I want the server side code to be executed only when the user click Yes. 我希望仅在用户单击“是”时才执行服务器端代码。
I am able to display the confirm box but my server side code is not executed. 我能够显示确认框,但是我的服务器端代码未执行。
Please help. 请帮忙。
Client Code 客户代码
var confirmed = false;
if (!confirmed) {
new Messi('Do you want to update status ?.', {
title: 'Confirm',
buttons: [{ id: 0, label: 'Yes', val: 'Y' }, { id: 1, label: 'No', val: 'N' }],
callback: function (val) {
confirmed=val;
}
}
);
}
return confirmed;
This is my code. 这是我的代码。
Server Code 服务器代码
<asp:Button ID="Button2" CssClass="myButton2" runat="server" OnClientClick="if( ! Button2Confirm()) return false;" Text="Update" Width="117px" OnClick="Button2_Click" />
Why do you use a jQuery Ajax call for this situation. 为什么在这种情况下使用jQuery Ajax调用。
Please see this below example of a sample jQuery Ajax call 请参见下面的示例jQuery Ajax调用示例
http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx
var confirmed = false;
if (!confirmed) {
new Messi('Do you want to update status ?.', {
title: 'Confirm',
buttons: [{ id: 0, label: 'Yes', val: 'Y' }, { id: 1, label: 'No', val: 'N' }],
callback: function (val) {
$.ajax({
type: "POST",
url: "Your_webpage.aspx/your_web_method",
data: '{name: confirmed }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response.d);
}
});
}
}
);
}
return confirmed;
:) :)
Your 你的
return confirmed;
is run before the button callback so this function will always return false. 在按钮回调之前运行,因此此函数将始终返回false。 You must call your server side code in the callback function:
您必须在回调函数中调用服务器端代码:
new Messi('Do you want to update status ?.', {
title: 'Confirm',
buttons: [{ id: 0, label: 'Yes', val: 'Y' }, { id: 1, label: 'No', val: 'N' }],
callback: function (val) {
if(val == 'Y')
// call server side
}
}
);
To call your button click you could use things like: 要调用您的按钮,可以使用以下方法:
__doPostBack('Button2','');
in JavaScript but I suggest you take a look at jQuery ajax and ASP.NET Web API . 使用JavaScript,但我建议您看看jQuery ajax和ASP.NET Web API 。
If you want to do it in OnClientClick you can't use your plugin and must go back to JavaScript confirm funcion: 如果要在OnClientClick中执行此操作,则不能使用插件,而必须返回JavaScript确认功能:
OnClientClick="return confirm("Are you sure?");"
confirm will halt execution until user presses one of the buttons. 确认将停止执行,直到用户按下其中一个按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.