简体   繁体   English

客户端回调和Ajax页面方法之间的区别 - ASP.NET

[英]Difference between Client Callbacks and Ajax Page Methods - ASP.NET

Based on my understanding, both of them essentially do the same thing (lets us execute a server side method from JS). 根据我的理解,它们都基本上做同样的事情(让我们从JS执行服务器端方法)。 Are there any differences? 有什么不同吗?

Also, Ajax Page Methods can be implemented either using JQuery or using ScriptManager. 此外,Ajax页面方法可以使用JQuery或使用ScriptManager实现。 Which one is preferred and why?? 哪个是首选,为什么?

**BOUNTY: Adding a bounty to get clear explanation of the question. ** BOUNTY:添加赏金以获得对问题的清晰解释。 Thanks ** 谢谢 **

Fundamentally, Client Callbacks and Ajax Page Methods are doing the same thing. 从根本上说, 客户端回调和Ajax页面方法正在做同样的事情。 They use an XMLHttpRequest object to send a request (usually asynchronous) to some URL, get the results of that request, then execute a callback method you've provided ( callback with a lowercase c ), passing the results of the request to your method. 他们使用XMLHttpRequest对象向某个URL发送请求(通常是异步的),获取该请求的结果,然后执行您提供的回调方法(使用小写c 回调 ),将请求的结果传递给您的方法。

Having said that, there is one big difference between the two approaches: 话虽如此,这两种方法之间有一个很大的区别:

  • Page Methods are implemented as static methods on your page. 页面方法在页面上以静态方法的形式实现。 Your page class is just a convenient container for these methods, which could really be hosted anywhere (a web service, a custom HttpHandler , etc.). 您的页面类只是这些方法的一个方便容器,它们可以真正托管在任何地方(Web服务,自定义HttpHandler等)。 Since no instance is ever going to be constructed, the client doesn't have to send ViewState data and Asp.Net doesn't have to run through the Page 's lifecycle. 由于不会构建任何实例,因此客户端不必发送ViewState数据,并且Asp.Net不必遍历Page的生命周期。 The flip side is that you don't have access to to your Page class's instance methods and properties. 另一方面,您无权访问Page类的实例方法和属性。 However, in many cases you can work around this by refactoring instance methods into static methods. 但是,在许多情况下,您可以通过将实例方法重构为静态方法来解决此问题。 (See this article for more information.) (有关更多信息,请参阅此文章 。)

  • Client Callbacks are implemented as instance methods on your page. 客户端回调在页面上实现为实例方法。 They have access to other instance methods on your page, including stuff stored in ViewState . 他们可以访问页面上的其他实例方法,包括存储在ViewState东西。 This is convenient, but comes at a price: in order to build the Page instance, the client has to send a relatively large amount of data to the server and has to run through a fair chunk of the page lifecycle. 这很方便,但需要付出代价:为了构建Page实例,客户端必须向服务器发送相对大量的数据,并且必须运行整个页面生命周期的大部分时间。 ( This article has a nice diagram showing which parts.) 这篇文章有一个很好的图表显示哪些部分。)

Apart from that, the cost of setting them up varies quite a bit and clients use them differently: 除此之外,设置它们的成本差别很大,客户使用它们的方式不同:

  • Client Callbacks require a fair amount of idiosyncratic scaffolding code that is intimately coupled to Asp.Net (as shown in the link above). 客户端回调需要大量与Asp.Net紧密耦合的特殊脚手架代码(如上面的链接所示)。 Given the much easier alternatives we have now, I'm tempted to say that this technology is obsolete (for new development). 鉴于我们现在拥有的更容易的替代方案,我很想说这项技术已经过时(对于新开发)。

  • Calling page methods using ScriptManager requires less setup than Client Callbacks: you just have to pop a ScriptManager onto your page, set EnablePageMethods = true , then access your page methods through the proxy the PageMethods proxy. 使用ScriptManager调用页面方法所需的设置少于客户机回调:您只需将ScriptManager弹出到页面上,设置EnablePageMethods = true ,然后通过代理访问PageMethods代理的页面方法。

  • Calling page methods using jQuery only requires you to link the jQuery library (and familiarity with jQuery, of course). 使用jQuery调用页面方法只需要链接jQuery库(当然也熟悉jQuery)。

I prefer to use jQuery to access page methods because it's independent of the server framework and exposes just the right amount of implementation details, but it's really just a matter of taste. 我更喜欢使用jQuery来访问页面方法,因为它独立于服务器框架并且只暴露了适当数量的实现细节,但这只是一个品味问题。 If you go with ScriptManager , its proxy makes the page method calls a little easier on the eyes, which some might consider more important. 如果你使用ScriptManager ,它的代理使得页面方法调用在眼睛上更容易一些,有些人可能认为更重要。

I would say there are differences, but would tend to say do it the way you feel more comfortable with. 我会说有差异,但往往会说你觉得更舒服的方式。

I have used both approaches, and having jQuery calls from the page is generally faster. 我已经使用了这两种方法,并且从页面调用jQuery通常更快。 I write an ashx handler that does the job the jquery call needs (query the database, process something, etc.) and call it from the page. 我编写了一个ashx处理程序来完成jquery调用所需的工作(查询数据库,处理某些内容等)并从页面调用它。 I wouldn't use an aspx page for a jQuery call, because you're sending a lot of info that you won't need at all. 我不会使用aspx页面进行jQuery调用,因为你发送了很多你根本不需要的信息。 The difference/ benefit of using an Ajax.Net call is that you don't need to build another page to process things, you can use the same page events to do it. 使用Ajax.Net调用的不同/好处是您不需要构建另一个页面来处理事物,您可以使用相同的页面事件来执行它。

For example, if you need to fill a second drop down list using the selected value on a first one, you could use Ajax.Net to call the SelectedIndexChanged in the page code behind and when it fires go Page_Load, SelectedIndexChanged, Page_PreRender and so on. 例如,如果您需要使用第一个选定值填充第二个下拉列表,您可以使用Ajax.Net在后面的页面代码中调用SelectedIndexChanged,当它触发时,转到Page_Load,SelectedIndexChanged,Page_PreRender等等。 In the event method you'd query the db and fill the second ddl. 在事件方法中,您将查询数据库并填充第二个ddl。

With jQuery that could be a bit different. 使用jQuery可能会有所不同。 You make your call to an ashx handler, the handler is just a server method that do the magic and return data in the form you want to have (json, array of strings, xml, etc) and fill the second ddl using javascript. 你调用一个ashx处理程序,处理程序只是一个服务器方法,可以执行魔术并以你想要的形式返回数据(json,字符串数组,xml等),并使用javascript填充第二个ddl。 As I told you before, some people doesn't feel too mcuh comfortable with Client code and tend to do it in the server, but I always say that you need to use the correct tool for the right job, so know your tools and apply them wisely. 正如我之前告诉过你的那样,有些人对客户端代码感觉不太舒服,而且往往会在服务器上做到这一点,但我总是说你需要使用正确的工具来做正确的工作,所以要知道你的工具并申请聪明地说。

If you want to know more about ASP.Net, ASHX handlers and jQuery, you can read a post that I wrote about it. 如果你想了解更多关于ASP.Net,ASHX处理程序和jQuery的信息,你可以阅读我写的关于它的帖子

Hope it helps.- 希望能帮助到你。-

They are essentially the same. 它们基本相同。 Both : 两者

  1. Setup a webservice for you that the javascript for the control can call. 为您设置一个Web服务,控件的javascript可以调用。
  2. Provide asynchronous respons e without involving the page lifecycle. 提供异步响应,而不涉及页面生命周期。

They are different: 它们是不同的:

  1. Page Methods simply require that you decorate a static method with an attribute and you are done. 页面方法只需要用属性装饰静态方法就可以了。 The rest of the magic is handled by HTTP Handlers and Modules. 其余的魔法由HTTP处理程序和模块处理。 Callbacks require you implement a few interfaces and handle the async event handlers yourself. 回调要求您实现一些接口并自己处理异步事件处理程序 I find them to be a little more of a pain. 我发现它们更像是一种痛苦。
  2. Callbacks only work with certain controls . 回调仅适用于某些控件 Calling page methods allows you to affect any control through custom javascript. 调用页面方法允许您通过自定义javascript影响任何控件。 Callbacks have a slight advantage here in that the client-side behavior is already written and fixed. 回调在这方面略有优势,因为客户端行为已经被编写和修复。 With page methods you have more flexibility , though (the behavior on the client side is determined by you). 使用页面方法,您可以获得更大的灵活性 (客户端的行为由您决定)。

There are a few other differences, but these are the basics. 还有一些其他差异,但这些是基础知识。 My understanding is that client callbacks tend to perform as well as Page methods, but are not used as much becuase they are only available in certain situations, whereas a Page Method is always a valid avenue. 我的理解是客户端回调往往与Page方法一样好,但由于它们仅在某些情况下可用,因此不会使用它们,而Page方法始终是一个有效的途径。

As for the ScriptManager vs. JQuery question, my feeling here is it's about taste more than anything. 至于ScriptManager与JQuery的问题,我在这里的感觉是它的味道比什么都重要。 I like JQuery's syntax and I feel like it performs better, but in the grand scheme of things the most expensive thing is the XmlHttpRequest... after that the execution of the javascript is likely to be insignificant in difference next to that. 我喜欢JQuery的语法,我觉得它表现得更好,但在宏观方案中,最昂贵的东西是XmlHttpRequest ......之后,javascript的执行可能在旁边的差异中无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM