ViewState concept in ASP. NET

Source: Internet
Author: User

I remember that a few years ago, when ASP. NET emerged, a "giant" appeared in the Web development framework across the world. WebForms, a development framework that everyone seemed to be able to master, almost instantly became popular. If someone is still using the traditional ASP control and performance mixed development method, it seems to have become much vulgar now. As a result, many people have learned how to drag controls and bind them. There are more and more "Web developers.

I don't know when the Rails framework began to become popular with RoR. Monorail also appeared in the. NET community, and the criticism of WebForms gradually grew. Now Microsoft has launched its own ASP-based. in the MVC Framework of the NET platform, many opponents of WebForms seem more confident: Even Microsoft has abandoned WebForms, proving that WebForms should indeed exit the historical stage, I have also heard something like "WebForms is not suitable for Web development and is already recognized as a fact. Not to mention whether Microsoft launched MVC means that it has abandoned WebForms. I think most of them are just "follow suit" from the disadvantages of WebForms that the MVC followers "Never forget ", just like WebForms, which was sought after by many people in the past.

However, I must admit that my understanding of ASP. net mvc is limited to what Scott Gu wrote. So far, I have not downloaded ASP. NET 3.5 Extensions CTP. RoR and Monorail are limited to some materials and examples and have never written a line of code. According to my "standards", I am not qualified to comment on the advantages and disadvantages of the MVC framework. However, I still want to write this article, because I will only reverse WebForms, and will not "degrade" the MVC framework. I just want to prove that WebForms's shortcomings are actually shortcomings, or the developers themselves did not take advantage of this tool. Therefore, based on my experience, I will respond to common WebForms accusations. If you have any improper wording, please forgive me.

The practices I mentioned below are all tested in the actual development process, such as the cooperation between developers and art engineers). They may not be the best, but I think they are still good.

I. ViewState Concept

HTTP is a stateless protocol, so ASP. NET introduces the ViewState concept, so that the status of the page control can be restored when the data is re-posted back to the page. Therefore, many rich functions are available, for example, some complex control events. However, the problem caused by ViewState is that, if improperly used, the page size will increase a lot. Too much data transmitted over the network will naturally affect the performance.

But is ViewState really required? I am very responsible for saying that most Web application pages today contain almost a large number of links. clicking a link will jump to a new page that is completely irrelevant to the current page, in this case, what is the use of ViewState on the page? Therefore, if I create a new Web project, the first thing I do is go to the Web. in config, enableViewState is globally disabled, and enableSessionState is also disabled. This is also one of the factors that affect performance. stateless also facilitates load balancing at the Web server layer ).

Someone once refuted me and disabled ViewState. What is the significance of using WebForm? My answer is: it makes a lot of sense. WebForm provides a control model that enables me to set or read values in a text box by "everyone can understand and write. I can easily respond to different button events to write various business logic triggering. This means that the development of WebForms is still very simple and clear to a certain extent. It is always correct not to "Abuse" it ).

Hmm? Didn't you say that the control event can be used only when ViewState is maintained? How can I retrieve the status from the control without ViewState? Note that what I mentioned previously is "complex events ". What are complex events? The TextChange event of TextBox is a complex event, while the Command event of GridView is a complex event, but the Click Event of the Button is a simple event, the status of each row of data in the GridView is "complex", while that of the TextBox is "simple ". "Complex state" and "complex events" require ViewState, because the "controls" related to them are ASP. NET, but "simple events" and "simple state" are based on the data that "inevitably" will be submitted on the page. They can naturally be used. In my ASP. in the development process of NET, almost all "simple events" and "simple state" are used ", however, giving up "complex events" and "complex states" does not cause me any problems.

When someone gives us 10 gifts and only 4 of them are what I need, why can't I simply give up the remaining 6, I would like to thank those who only gave us three gifts and accuse the former? You need to know that he is not malicious, and that the remaining six pieces have not caused us any trouble.

But people are so strange.

Ii. Performance

A major feature of WebForms is its powerful component model, which is often synonymous with "complicated. This component model contains a theme called "Life Cycle", which has been accused by many people as a performance killer. This complex life cycle is indeed implemented over and over again without any need. It does seem to have resulted in a "waste", but is it actually at the "killer" level?

If you think this component model is a performance killer, it is better to write a page with 1000 built-in dynamic Button controls and deploy them on the server. I ensure that the operation is fast. If there are not enough 1000 widgets, you can try 3000, 5000, or even 10000 widgets. On which page do you have more controls than this one? But how many pages will your performance be higher than it? There are also articles saying that "use as few server-side controls as possible and add runat = server to HTML controls at most", which makes no reason: an HTML control with runat = server added, it has become a server-side control. The common HTML is only processed as common text in the control tree. In the control tree, a Literal is used to save the "character ". ASP. NET does not care about the specific content.

There are various causes of performance problems. Before you explore and optimize performance problems, you must identify the performance bottleneck to remedy the problem. In some aspects, if we extract the public part into a new method, the execution of the call Command will be performed once more, and the performance will be reduced, but I believe no one will copy the same code everywhere. In the Web applications we are exposed to, most of the performance bottlenecks are database access or external Service access ), if you execute one more database query operation, you may be able to withstand 0.1 billion reference copies in the memory. I believe that if the performance of an ASP. NET application is not high, it is almost impossible to cause problems due to component models or lifecycles.

Since most Web application bottlenecks are related to database access, how can this problem be solved? The most direct method should be to optimize database queries, but the most critical possibility is cache. June doesn't know that every lecture on Web Application Performance Optimization places Cache on the top of the top, because this is indeed one of the most effective optimization methods. In a highly concurrent Web application, caching some data for one minute can also bring considerable performance improvement. Other methods may still generate static pages, which are not faster than the access speed). asynchronous calling, for example, a newly published article, does not matter if it can be searched in a few minutes, so why must we write data or create indexes synchronously and instantly ?) Separate servers with different roles can perform targeted configuration for different servers, such as separating image servers), and the importance of implementing Server Load balancer stateless on Web servers can be seen ), vertical cutting of databases to speed up the amount of data loaded in the memory can improve query performance, and after vertical cutting, multiple database servers can be used to share the pressure), horizontal sharding, split data into different databases to scale out to reduce the load on each server and improve performance ), for data redundancy or Master-Slave, the performance of write operations is slightly reduced, and the performance of Data Reading is improved. Most common Web Applications "read" much more than "write.

Of course, I have mentioned application implementation and architecture. In fact, developing a high-performance Web application involves hardware, software, and operating systems, I will not explain it here. In fact, I am still exploring it ). In fact, what I want to talk about here is that the key to developing high-performance Web applications is mostly not related to the specific implementation technology: as long as the "Implementation" is correct, the practices are mostly the same, whether it is SQL Server/Oracle, Windows/Linux or ASP. NET/RoR, which is essentially similar. Ruby and C # differ by ten times in terms of performance and are you sure you want to verify?) Can you develop high-performance Web applications? The following describes the ViewState concept in ASP. NET.

  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.