I have a delete link for every row in my grid. It looks like this:
<a href="deleteLink" onclick="return OnDeleteRecord(122885);"><font color="#CC0000">Delete</font></a>
My objective is to check if a record has been printed or not by making an Ajax call to an ASP.net Web Method two times. Once before the user confirms delete, and once again after they click confirm.
I easily took care of this by creating a synchronous AJAX call, but I read everywhere that synchronous AJAX calls are horrible and depricated.
After looking here I decided to use JQuery deferred objects to grab the response values
What I did was this:
After clicking "Delete" OnDeleteRecordClick(recordID) gets called.
First, I make my Ajax Call, then return false (notice the end of the method)
Then, after I get the first response, depending on if the record is not printed or not, I show a javascript confirmation, and make my ajax call again, Then return false again.
After the second response, if the record is unprinted, I return true, to delete the record.
Here's my OnDeleteRecord:
function OnDeleteRecordClick(recordID) {
// This action takes place asynchronously
BeforeRecordDelete (recordID)
.done(function (r) {
if (r.d == 1) {
// Check isPrinted before clicking "Confirm"
alert("You can't delete this record printed.");
return false;
} else {
if (ConfirmDelete('record')) {
// Check isPrinted again after clicking "Confirm"
BeforeRecordDelete (recordID)
.done(function (r) {
if (r.d == 1) {
alert("You can't delete this record because it has been printed.");
return false;
} else {
// Proceed to delete
return true;
}
})
.fail(function (x) {
Alert("server failed");
return false
});
// Return false until we get a response from the server
return false;
} else {
// User clicks "Cancel"
return true;
}
}
})
.fail(function (x) {
// Tell the user something bad happened
Alert("server failed");
return false;
});
// Return false until we get a response from the server
return false;
}
Here's my async AJAX call:
BeforeRecordDelete = function (recordID) {
return $.ajax({
type: "POST",
url: "RecordList.aspx/IsRecordPrinted",
contentType: "application/json; charset=utf-8",
data: '{RecordID: "' + RecordID + '"}',
dataType: "json"
});
}
Here's my problem: When I return false two times, then return true, the hyperlink wouldn't work.
My questions are:
Can I even do this to a hyperlink? (Returning multiple values to OnClick)
Is there a better way to do this? I really want to do this asynchronously, but my brain hurts.