Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET AJAX - ScriptManagerProxy and handling html button clicks
    primarykey
    data
    text
    <p>I am trying to figure out how to accomplish the following:</p> <ul> <li>Handle client-side click event of button - prevent postback if result of javascript call is false (e.g. <code>onclick="js:return IsThisAllowed()"</code>)</li> <li>Javascript method will return true or false, depending on result of call to generated web service method using ScriptManagerProxy</li> </ul> <p>The code below is simplified down to the main components that I am referring to in this question. I can alert() the return value no problem, but the part I'm having trouble with is how I can return the result of the web service method call back to the button onclick handler? Since the function Finish() is called in a separate thread upon success of what I assume is the XmlHttp.send() call, how can I send the value of the response as the return value of the javascript method? Am I going about this all wrong?</p> <p>I've used similar code many times before to update the DOM with the return values, such as within a <code>&lt;div&gt;</code>, but never had to worry about capturing the return value and using it within my script.</p> <p>What I have so far:</p> <p>.NET web service method (VB.NET):</p> <pre><code>&lt;WebMethod()&gt; _ &lt;ScriptMethod()&gt; _ Public Function IsAllowed() As Boolean Return True End Function </code></pre> <p>.NET code for the page to generate AJAX web service methods, and html button</p> <pre><code>&lt;asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"&gt; &lt;Services&gt; &lt;asp:ServiceReference Path="~/WebServices/MyWebService.asmx" /&gt; &lt;/Services&gt; &lt;/asp:ScriptManagerProxy&gt; &lt;asp:Button ID="_btn" runat="server" OnClientClick="js:return IsAllowed();" /&gt; </code></pre> <p>Javascript</p> <pre><code>&lt;script type="text/javascript"&gt; 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() { } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload