使用 AjaxPro 传递变量

发布于 2024-07-11 03:40:46 字数 1072 浏览 5 评论 0原文

前几天我发现了这个 AJAX .NET 框架,到目前为止我很享受使用它。 然而,我无法弄清楚的一件事(文档相当糟糕)是如何通过 AJAX 脚本传递变量。 这就是我到目前为止所拥有的:

//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>

我还有其他几个运行良好的测试函数,并且它们执行相对更复杂的操作。 有人知道如何使用 AjaxPro 传递变量吗? 也欢迎其他方法!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疯狂的代价 2024-07-18 03:40:46

使用 AjaxPro,您首先传递参数,然后传递回调函数作为最后一个参数。 如果您在此处使用匿名函数,您可以访问响应和原始值。 它看起来像这样:

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

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);
}
巷雨优美回忆 2024-07-18 03:40:46

我知道这是一个旧的,但我一直在使用 AjaxPro,并且仍然将它与 .NET 4 一起使用 - 它仍然有效。

如果您想通过回调传递额外的变量并将其返回到结果中,请在回调之后传递该值并使用 res.context 来检索它。

像这样:

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

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

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;
}
蓝颜夕 2024-07-18 03:40:46

自从我使用该框架(版本 6.9.22.2,所以事情可能发生了一些变化)以来已经有一段时间了 - 你看过 ASP.NET AJAX 框架? 我不确定自发布以来 Michael 在这方面做了多少工作 - 当然,他关于它的博客文章在 2007 年 7 月左右就枯竭了。

无论如何,如果我正确理解你的问题,你想要:

  1. 将一个值传递给你的服务器端方法
  2. 在回调方法中的某处使用该值

首先,要将变量传递给您的方法,它们不应该出现在回调函数名称之前吗?

要检索回调中的初始值,我可以:

  1. 将它们作为 JSON 返回对象的一部分返回
  2. 将它们存储在两个函数范围之外的变量中

使用选项 2,您将得到:

<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>

这样做的主要缺点是如果用户在您仍在处理第一个请求时使用不同的值触发您的“doTest”方法,则在 doTest_callback 中查询时,initialValue 的值很可能已经更改。

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.

静待花开 2024-07-18 03:40:46

唔。 您注意到任何 JavaScript 错误吗? 如果您安装了 Firefox 和 FireBug,JavaScript 控制台中是否有任何错误?

听起来组件生成的 JS 文件有问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文