简体   繁体   English

updatepanel与页面方法

[英]updatepanel vs page methods

I use update panels all the time when i wanna to update specific part of my page but recently i face performance problems ( i mean it 's slow in rendering the intended control and sometimes it doesn't work and need multiple click to work !! 我一直在使用update panels ,当我想要更新我的页面的特定部分但最近我遇到性能问题(我的意思是它渲染预期的控件很慢,有时它不起作用,需要多次点击工作!

so my question is : 所以我的问题是:

  • Is the page method could be considered as an efficient alternative to the update panel and do the ajax magic ? 页面方法是否可以被视为更新面板的有效替代方案并执行ajax魔术?
  • What are the other alternatives? 还有什么其他选择?

please if possible a simple example to clarify how to replace the update panel using with page methods ? 请尽可能一个简单的例子来阐明如何使用页面方法替换更新面板?

I used to be like you some years ago, I used to use UpdatePanel to gain performance, to have the wrong idea I was increasing the performance of my applications ... 几年前我曾经像你一样,我曾经使用UpdatePanel 获得性能,错误的想法我正在提高我的应用程序的性能 ......

Well I was totally wrong, UpdatePanel is the root of all UI-evil, first of all it hides the complexity of using AJAX which makes it easy for most of us, giving us the wrong idea that we are creating responsive applications, which is worst than if we weren't using it at all (that's the main reason I used to use it in all my pages, and I am sure that's the reason why many developers use it... 'cos it's easy). 我完全错了, UpdatePanel是所有UI邪恶的根源,首先它隐藏了使用AJAX的复杂性,这让我们大多数人都很容易,让我们错误地认为我们正在创建响应式应用程序,这是最糟糕的比如我们根本不使用它(这是我以前在我所有页面中使用它的主要原因,我确信这就是许多开发人员使用它的原因......因为它很容易)。

Consider the following articles: 请考虑以下文章:

When you understand what the UpdatePanel really does against a simple call to a PageMethod or a REST WCF Service, you will see the huge difference between them. 当您了解UpdatePanel对简单调用PageMethod或REST WCF服务的真正作用时,您将看到它们之间的巨大差异。

  • UpdatePanel . UpdatePanel When you perform a post from an UpdatePanel , the whole page life-cycle has to be executed, this means, it requires to send all the page ViewState on each post, when your page grows in complexity with several controls, the ViewState will certainly be huge and this will certainly be a performance issue. 当您从UpdatePanel执行帖子时,必须执行整个页面生命周期,这意味着,它需要在每个帖子上发送所有页面ViewState,当您的页面增加复杂的几个控件时,ViewState肯定会巨大的,这肯定会是一个性能问题。 Using them you only gain partial rendering , the controls inside your UpdatePanel will be rendered without a full post back although you need to send the whole ViewState on each request. 使用它们只能获得部分渲染 ,尽管您需要在每个请求上发送整个ViewState,但UpdatePanel的控件将在没有完整回发的情况下呈现。

  • PageMethod . PageMethod Page methods are static , they are called like if they were a service method, they do not need to create the whole page life-cycle in order to be executed, therefore, they execute faster. 页面方法是static ,它们被称为如果它们是服务方法,它们不需要创建整个页面生命周期以便执行,因此,它们执行得更快。

So it would seem that using PageMethods would be the solution, the problem is that PageMethods are usually used to return JSON objects which means, that you will have to render these objects manually . 因此,使用PageMethods似乎是解决方案,问题是PageMethods通常用于返回JSON对象,这意味着您必须手动呈现这些对象。 This means that if you want to get rid-off all your UpdatePanel you will have to change the controls used in your views, you won't be able to use the GridView out-of-the-box for example, instead you would have to change it for the JQGrid (or similars). 这意味着如果你想要摆脱你所有的UpdatePanel你将不得不更改你的视图中使用的控件,你将无法使用GridView开箱即用,相反,你会有为JQGrid (或类似物)改变它。

This is natural if you are creating a MVC application, but with traditional ASP.Net this is not straightforward. 如果您正在创建MVC应用程序,这是很自然的,但对于传统的ASP.Net,这并不简单。

You also need to consider something very important, the ViewState is validated by default on each post , you can turn it off, but it is not recommended if you want to be sure your ViewState has not been corrupted ( take a look at this question ). 您还需要考虑一些非常重要的事情, 默认情况下会在每个帖子上验证ViewState ,您可以将其关闭,但如果您想确保您的ViewState没有被破坏,则不建议这样做( 请看一下这个问题 ) 。

Consider this example, you have two DropDownList controls, (named: ddl1, ddl2) ddl2 depends on ddl1 so using the SelectedIndexChanged event you fill the second drop down list. 考虑这个例子,你有两个DropDownList控件,(名为:ddl1,ddl2) ddl2依赖于ddl1,所以使用SelectedIndexChanged事件填充第二个下拉列表。 But if you attempt to do the same using AJAX calls (without an UpdatePanel ), you will face two problems 但是如果你尝试使用AJAX调用(没有UpdatePanel )来做同样的事情,你将面临两个问题

  • Rendering, you need to manually add objects to the HTML select control representing the DropDownList . 渲染时,您需要手动将对象添加到表示DropDownList的HTML select控件。 You could use a third party framework to bind these controls using javascript, I can recommend you knockoutjs (it's awesome) 你可以使用第三方框架使用javascript绑定这些控件,我可以推荐你knockoutjs (它太棒了)

  • This is the problem. 这就是问题。 After you have changed the content of the second DropDownList using javascript, you cannot do a simple post to your page because the ViewState will not be valid , and you will see the following exception: 使用javascript更改了第二个DropDownList的内容后, 您无法对页面进行简单的发布,因为ViewState将无效 ,您将看到以下异常:

Invalid postback or callback argument. 无效的回发或回调参数。

The workaround is to specify which values will be valid in the server side, in order to do that you need to override the page Render method and specify each one of the values of the second drop down list , but this will increase the page size and obviously, this is not a good option 解决方法是指定哪些值在服务器端有效,为了做到这一点,您需要覆盖页面Render方法并指定第二个下拉列表中的每个值 ,但这会增加页面大小和显然,这不是一个好的选择

Take a look: 看一看:

So as a summary, if you want to get rid-off all your UpdatePanel controls, you will need to replace the existing server controls for javascript-friendly controls. 总而言之,如果您希望摆脱所有UpdatePanel控件,则需要替换现有的服务器控件以实现对javascript友好的控件。 Also remmeber that if you do that, instead of relying on the page post mechanism, you would have to use AJAX to perform operations on the server, otherwise, you will get the Invalid postback or callback argument. 另请注意,如果您这样做,而不是依赖于页面发布机制,则必须使用AJAX在服务器上执行操作,否则,您将获得无效的回发或回调参数。 exception. 例外。 In other words it would be better to consider moving to a MVC application if possible. 换句话说,如果可能的话,考虑转移到MVC应用程序会更好。

You might read about the coming WebAPI in .NET 4.5. 您可能会阅读.NET 4.5中即将发布的WebAPI。 It's for WebForms as well as MVC and may be a viable solution to your problem if you can wait on 4.5. 它适用于WebForms以及MVC,如果你可以等待4.5,它可能是你问题的可行解决方案。

Just use it in combination with any jQuery template engine. 只需将它与任何jQuery模板引擎结合使用即可。

http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx

There is an alternative to UpdatePanels, but still using PageMethods. UpdatePanels有一个替代方案,但仍然使用PageMethods。 It is a combination between jQuery and jQuery templates . 它是jQuery和jQuery模板的组合。 It is proven to be faster than the UpdatePanels. 它被证明比UpdatePanel更快。 Further reading on the resource below, where you can find more articles dedicated to this topic. 进一步阅读下面的资源,在那里您可以找到更多专门讨论该主题的文章。

http://encosia.com/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/ http://encosia.com/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/

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

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