I want to display a confirm dialog box on my asp.net website before the user upload the data.
I am using http://marcosesperon.es/apps/messi/ plugin for dialog box.
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.
Please see this below example of a sample jQuery Ajax call
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. 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 .
If you want to do it in OnClientClick you can't use your plugin and must go back to JavaScript confirm funcion:
OnClientClick="return confirm("Are you sure?");"
confirm will halt execution until user presses one of the buttons.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.