如何获取 jQuery PageMethods 函数的结果
我已经发布了与此问题类似的问题,并且可以自己回答,但解决方案并不完全令我满意。
使用 Asp.Net/C#,我有一个按钮,如果它可以执行一些服务器端代码 通过客户端验证。我想这样做,因为对于我的应用程序,我认为它最适合。
<asp:Button runat="server" ID="addBtn" Text="Add" OnClientClick="if(validateEntry()== false){return false;}" OnClick="addBtn_Click"/>
我调用一个名为 validateEntry() 的 jQuery 函数,它使用 PageMethods 来获取一些 来自服务器的数据。这些数据对于验证过程是必需的。
function validateEntry() {
PageMethods.CheckEntry(params, function(result){//Evaluate result of PageMethods}
}
我的问题是我不知道如何正确处理从 PageMethods 收到的数据。 我只能将它们交给可以使用它们的子功能。但我需要 变量中的数据执行返回 false。
I have posted a similar question to this problem and could answer it myself, but the solution does not fully satisfy me.
Using Asp.Net/C#, I have a button which will perform some server-side code if it
passes a client-side validation. I want to do it this way because for my application, I think it suits best.
<asp:Button runat="server" ID="addBtn" Text="Add" OnClientClick="if(validateEntry()== false){return false;}" OnClick="addBtn_Click"/>
I call a jQuery function named validateEntry() which uses PageMethods to get some
data from the server. The data are necessary for the validation process.
function validateEntry() {
PageMethods.CheckEntry(params, function(result){//Evaluate result of PageMethods}
}
My problem is I do not know how to handle the data received from PageMethods properly.
I am only able to hand them over to a sub-function which can use them. But I need the
data in sort of variable to perform a return false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经发布了一个运行示例,如何使用 jquery 这里。
I have posted a running example how to consume page methods from jquery here.
我找到了一个解决方案,但如果有更简单的解决方案,我将不胜感激。
我在页面上使用隐藏字段,并在子函数中写入结果
从页面方法进入字段:
之后,我可以从字段中读取结果并在主函数中使用它。
I worked out a solution, but would be thankful for easier ones.
I use a hidden field on the page and in the sub-function, I write the result
from the page method into the field:
Afterwards, I can read the result from the field to use it in the main function.
鉴于 PageMethods.CheckEntry 不延迟地运行传递的函数,您可以执行以下操作:
如果您尝试此操作并且未显示“foo”,则该函数会延迟,您需要重新考虑如何执行此操作或提供更多信息,以便我们可以帮助你。
Given that PageMethods.CheckEntry runs the passed function without delay you can do this:
If you try this and "foo" is not displayed, then the function is delayed and you need to rethink how to do this or provide more information so that we can help you.