I am currently working on a solution for the following issue:
- Managing the client-side click event of a button - preventing postback if the JavaScript call returns false (e.g.
)onclick="js:return IsThisAllowed()"
- The JavaScript method will either return true or false based on the result of a call to a generated web service method using ScriptManagerProxy
The code snippet below has been simplified to focus on the key components related to my question. I am able to use alert() to display the return value, but I am struggling with how to pass the response from the web service method call back to the button's onclick handler. Since the function Finish() is executed in a separate thread upon successful completion of what seems to be the XmlHttp.send() call, how can I set the response value as the return value of the JavaScript method? Am I approaching this problem incorrectly?
I have previously used similar code to update the DOM with return values, like within a <div>
, however, I have never had to capture the return value and incorporate it into my script.
What I have tried so far:
.NET web service method (VB.NET):
<WebMethod()>
<ScriptMethod()>
Public Function IsAllowed() As Boolean
Return True
End Function
.NET code for the page to create AJAX web service methods, along with an HTML button
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebServices/MyWebService.asmx" />
</Services>
</asp:ScriptManagerProxy>
<asp:Button ID="_btn" runat="server" OnClientClick="js:return IsAllowed();" />
Javascript
<script type="text/javascript">
function IsAllowed(orderid) {
if (!processing) {
processing = true;
var ws = new MyWebService();
ws.IsAllowed(Finish, Error)
// return TRUE if call to IsAllowed returns true!!!
return false;
}
}
function Finish(result) {
alert(result)
}
function Error() {
}
</script>