问题
I haven't started writing any code for this program, but here's what I need to do in C#/ASP.NET, both of which I'm just starting to learn.
I have a DIV on a page that I want to update with information from an MS SQL Server every five seconds.
Would it be better to create my countdown timer on the JavaScript or C# side?
Would UpdatePanel or creating a Page Method be more efficient for updating the DIV with the database information?
Load times are a serious issue for this application, so the lighter and faster the solution, the better.
回答1:
1) You'll have to create the countdown timer on the client in javascript since the client is disconnected from your server-side code except when it explicitly sends requests to it.
2) UpdatePanel
will be less efficient, because it posts all of your page's form values (including any ViewState
or EventValidation
material), when all you might need to pass to the server is a few bytes (for an id, for example). However, this difference in efficiency may not be significant if your query times are high (or the cost of transporting/rendering the data is high).
回答2:
The UpdatePanel will also use the timer on client side to refresh periodically. So it is better to use some efficient WebMethod rather than sending and retrieving whole form values using UpdatePanel. For more information regarding pros and cons of UpdatePanel and WebMethods you should visit Dave's website (http://www.encosia.com)
回答3:
If you decide to use a web service, use WCF instead. ASMX is now considered legacy technology by Microsoft.
For skeptics, from "XML Web Services Created Using ASP.NET and XML Web Service Clients":
This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation (WCF).
来源:https://stackoverflow.com/questions/1165612/net-page-method-vs-updatepanel-which-is-better-for-updating-and-refreshing