Do not always think that asp.net mvc is better than asp.net web form-the web form page can also be small

Source: Internet
Author: User

I recently saw a misunderstanding of asp.net web form on the Internet. There are also a lot of discussions, So Microsoft ScottGu can also clarify. Asp.net mvc and web form are two parallel development modes. Many people criticize the web form a lot. The most important one is viewstate. For a web form page, the html source code is very large, mainly because the viewstate field hidden is relatively large, the page should be at least dozens of K. The page is a little complicated. The viewstate can be several megabytes in size. Such a large page is transmitted between the browser and the server, which is also a great pressure on the network bandwidth, therefore, the performance of asp.net webform must be greatly affected. This is actually a serious misunderstanding of asp.net web form. Here is a tip. Make the pages of asp.net web form as small as those of asp.net mvc. You can also enjoy the benefits of viewstate.

For example, a form that displays the record list. For example:

This is a simple page. Its html source code is:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

The viewstate of the hidden field is large in size. It has a great impact on the performance of applications.

You can use the following method to compress the html source code of a page.

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace EntLibSample{    public class BasePage : System.Web.UI.Page    {        protected override void SavePageStateToPersistenceMedium(object viewState)        {            Session["viewState" + this.Context.Request.FilePath] = viewState;        }        protected override object LoadPageStateFromPersistenceMedium()        {            if (Session["viewState" + this.Context.Request.FilePath] != null)            {                return Session["viewState" + this.Context.Request.FilePath];            }            return string.Empty;        }    }}

This BasePage class inherits the System. Web. UI. Page class. System. web. UI. the SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium methods in Page send viewstate content to the client browser. Therefore, each request sent to the server needs to send the huge viewstate field back to the server. When the server returns the result, then, the huge viewstate field is sent back. This has a great impact on the overall performance of the application. The BasePage class overrides these two methods. Put the viewstate content in the session and retrieve it from the session when it is used. This avoids the transmission of large viewstate content back and forth on the network.

All pages of the application inherit the BasePage class. This method can be applied to all asp.net web form pages.

The effect is as follows:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

The viewstate field has no content. In this way, the overall size of the html source code is much smaller. This improves the performance of applications. Only asp.net mvc used to have small pages. Now asp.net web form can also be used. At the same time, asp.net web form has a relatively high development efficiency and can also take advantage of viewstate. With these Comprehensive considerations, we should regain confidence in asp.net web form, continue your journey in asp.net web form.

 

 

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.