EDIT: My specific question is about the "return CheckStatus(this, event, 9000)". The CheckStatus function returns true, but the OnClick (server side) is never executed for some reason. I don't have a problem with reading the correct value from my Ajax request. This has been tested and works accordingly.
I'm using webforms on a legacy application. I'm trying to get an ImageButton to do a quick server-side check of something. When that condition is met, the user receives a confirm dialog. When he clicks "yes" (or "ok"), the form is posted, otherwise no action is done.
This should be simple enough, and yet something seems to be wrong with the following code, as the button never executes the OnClick event on the server.
ASP.NET imagebutton:
<asp:ImageButton ID="btnBook" runat="server" CommandName="book" ImageUrl="~/modulas/images/icon_forward.gif" OnClientClick=<%# "return CheckStatus(this, event, " & Eval("vacationid") & ");"%> />
Which becomes this in the browser:
<input type="image" name="btnBook" id="btnBook" src="images/icon_forward.gif" onclick="return CheckStatus(this, event, 7655);">
The javascript function that is run, is the following:
function doAjax(myUrl, vacId) {
var result;
$.ajax({
type: "GET",
async: false,
url: myUrl + '?vacId=' + vacId,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (ajaxResult) {
var resultCode = parseInt(ajaxResult.d);
if (resultCode > 0) {
result = confirm('Are you sure?');
}
},
error: function (message, message2, message3) {
alert("error!");
result = false;
}
});
return result;
}
function CheckStatus(obj, event, vacId) {
event.preventDefault();
var myUrl = "/GetVacationStatus";
return doAjax(myUrl, vacId);
}
On the server, the OnClick gets handled like this:
(the button is inside a gridview)
Protected Sub grdListing_RowDataBound1(sender As Object, e As GridViewRowEventArgs) Handles grdListing.RowDataBound
If e.CommandName = "book" then
Book()
End If
End Sub
Private Function Book() As Boolean
'Do something
End Function
I've omitted the real business logic. If I remove the javascript function from the button, then this code executes perfectly, but I also need the javascript code to work
Seems to me this should be pretty straight forward, but what is happening is I click "yes" in the confirm dialog, and nothing happens. I click "no" in the confirm dialog, and again nothing happens. And I'm at wit's end as to what could be wrong.
Does anyone see the problem, or does anyone have any more ideas of things I could try?
Thank you very much