Passing variables with AjaxPro

3.3k Views Asked by At

I found this AJAX .NET framework the other day, and so far I am enjoying working with it. However, one thing I can't figure out (the documentation is rather poor atm) is how to pass variables through the AJAX script. This is what I have so far:

//default2.aspx.vb
  <AjaxPro.AjaxMethod()> _
  Public Shared Function testSomething(ByVal value As String) As String
     Return value
  End Function
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     AjaxPro.Utility.RegisterTypeForAjax(GetType(Default2))
  End Sub

//default2.aspx
  <a href="#" onclick="doTest(1);">test something</a>
  <div id="temp" style="margin:15px 15px 0px 5px; padding:10px;"></div>
  <script type="text/javascript">
     function doTest(x){
         Default2.testSomething(doTest_callback,x)
     }
     function doTest_callback(res,x){
         alert(res.value);
         document.getElementById("temp").innerHTML = ">>>> " + x + " <<<<"
     }
  </script>

I have a couple of other test functions that work fine, and they do comparatively more complex operations. Anyone have any insight into how to pass variables with AjaxPro? Alternative methods are welcome as well!

4

There are 4 best solutions below

0
On

It's been a while since I used that framework (version 6.9.22.2, so things may have changed a little) - have you had a look at the ASP.NET AJAX framework? I'm not sure how much work Michael's done on this since that came out - certianly his blog posts about it dried up around July 2007.

Anyway, if I understand your question right, you want to:

  1. Pass a value to your server side method
  2. Use that value somewhere in your call back method

Firstly, to pass variables to your methods, shouldn't they come before the name of the callback function?

To retrieve the initial values in the callback, I either:

  1. Returned them as part of a JSON return object
  2. Stored them in a variable outside the scope of both functions

Going with option 2, you'd have:

<a href="#" onclick="doTest(1);">test something</a>
<div id="temp" style="margin:15px 15px 0px 5px; padding:10px;"></div>
<script type="text/javascript">
   var initialValue;

   function doTest(x){
     initialValue= x;
     Default2.testSomething(x, doTest_callback)
   }

   function doTest_callback(res){
     alert(res.value);
     document.getElementById("temp").innerHTML = ">>>> " + initialValue + " <<<<"
   }
</script>

The main downside with this, is that if the user fires your "doTest" method with a different value while you're still processing the first request, the value of initialValue could well have changed by the time it's queried in doTest_callback.

0
On

I know this is an old one, but I've been using AjaxPro forever and still use it with .NET 4 - It still does the trick.

If you want to pass an extra variable with the callback and get it back in the result, pass the value after the callback and use res.context to retrieve it.

Like this:

function CallSomething()
{
   DoServerSide.MethodName(arg1, arg2, CallbackFunction, ExtraVar);
}

function CallbackFunction(res)
{
  var result = res.value;
  var extra = res.context;
}
0
On

With AjaxPro you pass the parameters first and a callback function as the last parameter. If you use an anonymous function here you can get access to both the response and the original value. It would looks something like this:

function doTest(x){
    Default2.testSomething(x, function(res){
        doTest_callback(res.value, x);
    });
}
function doTest_callback(resonseValue,originalValue){
    alert("response: " + responseValue);
    alert("original: " + originalValue);
}
0
On

Hmm. Have you noticed any JavaScript errors? If you've got Firefox and FireBug installed, are there any errors in the JavaScript console?

It sounds like there's something wrong with the generated JS files from the component.