使用 AjaxPro 传递变量
前几天我发现了这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 AjaxPro,您首先传递参数,然后传递回调函数作为最后一个参数。 如果您在此处使用匿名函数,您可以访问响应和原始值。 它看起来像这样:
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:
我知道这是一个旧的,但我一直在使用 AjaxPro,并且仍然将它与 .NET 4 一起使用 - 它仍然有效。
如果您想通过回调传递额外的变量并将其返回到结果中,请在回调之后传递该值并使用 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:
自从我使用该框架(版本 6.9.22.2,所以事情可能发生了一些变化)以来已经有一段时间了 - 你看过 ASP.NET AJAX 框架? 我不确定自发布以来 Michael 在这方面做了多少工作 - 当然,他关于它的博客文章在 2007 年 7 月左右就枯竭了。
无论如何,如果我正确理解你的问题,你想要:
首先,要将变量传递给您的方法,它们不应该出现在回调函数名称之前吗?
要检索回调中的初始值,我可以:
使用选项 2,您将得到:
这样做的主要缺点是如果用户在您仍在处理第一个请求时使用不同的值触发您的“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:
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:
Going with option 2, you'd have:
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.
唔。 您注意到任何 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.