Passing Variables with AjaxPro

I found this AJAX.NET framework the other day and I still enjoy working with it. However, one thing that I cannot figure out (the documentation is rather poor) is how to pass the 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 several other test functions that work great and perform relatively more complex operations. Does anyone know how to pass variables using AjaxPro? Alternative methods are also welcome!

0


source to share


4 answers


With AjaxPro, you pass the parameters first and the callback function as the last parameter. If you are using an anonymous function, you can access both the response and the original value. It 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);
}

      

+1


source


I know this is old, but I've used AjaxPro forever and still use it with .NET 4. It still does the trick.

If you want to pass an additional variable with a callback and return it in the result, pass the value after the callback and use res.context to get it.



Like this:

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

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

      

+1


source


It's been a while since I've used this framework (version 6.9.22.2, so things might have changed a bit) - have you looked at the ASP.NET AJAX framework ? I'm not sure how much work Michael did with this, since how it turned out - strictly, his blog posts about it dried up in July 2007.

Anyway, if I understand your question correctly, you want:

  • Pass the value to your server side method
  • Use this value anywhere in your callback method

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

To get the initial values ​​in the callback, I either:

  • Returns them as part of the returned JSON object
  • Store them in a variable outside the scope of both functions

Going with option 2, you will 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 disadvantage of this is that if the user runs your "doTest" method with a different value while you are still processing the first request, the initialValue value may well have changed by the time they requested the doTest_callback request.

0


source


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

It seems there is something wrong with the generated JS file from the component.

0


source







All Articles