Asp. NET State engine

Source: Internet
Author: User

  1. The definition of state and its importance

    HTTP is stateless, and the Web server does not track requests made from specific browsers, each of which is independent of each other. Now assume that the page has two text boxes and a button for the login function, assuming the user entered the correct user name and the wrong password to log in, then the page will notify the login failure, then the user would like to login failed after the page has been automatically filled out the user name and do not want to re-enter, but by default, The Web page or control does not do it by itself, because each request is a separate request because the server does not populate the text box again after the postback, but simply processes the page in the way it first loads the page.

  2. Performance of the State engine

    On the page there are two button (SETDATEBTN&PLAINPOSTBACKBTN) and a label (Datelabel) control, SETDATEBTN is used to define the time of the Datelabel, PLAINPOSTBACKBTN simply triggers a postback without any action.

    <form id="Form1"runat="Server"> <div> <table> <tr> <td> <asp:label id="Datelabel"runat="Server"text=" label "/> </td> <td> <asp:button id="setdatebtn"runat="Server"text="SetDate"onclick="Setdatebtn_click"/> </td> </tr> <tr> <td></td> <td> <asp:button id="plainpostbackbtn"runat="Server"text="Plainpostback"onclick="Plainpostbackbtn_click"/> </td> </tr> </table> </div> </form>
            protected void Setdatebtn_click (object  sender, EventArgs e)        {            = DateTime.Now.ToString ( );        }         protected void Plainpostbackbtn_click (object  sender, EventArgs e)        {        }

    When you click Setdatebtn, the Label control displays the current time, and when you click Plainpostbackbtn, the text that the label control displays remains the same, because the ASP. NET State engine holds the label's state. However, when you set the label's EnableViewState to false about the control state, when you click Plainpostbackbtn, the Label control is displayed as its original value, label.

  3. How the State engine works

    The html<form> element is used to allow users to submit information from the browser to the server, and forms are submitted in two ways: The Post or Get,post method is used to add data to the body of the request, and the Get method attaches the data to the address of the requesting page. It is important that it encapsulates some form controls whose values are submitted back to the server together. When you click a button similar to a control that causes a postback, it causes a postback to be sent to the server, during which all relevant information in the form is submitted back to the server and can be used to refactor the page on the server. Next introduce the Hidden _viewstate field

    <input type="hidden" name="__viewstate " id= " __viewstate " value="rytxnjmgeufrtdvtz0mxbkdtivgfdy+sfr2xlbkklbgfjapqc5qqwozufcgz61ceg/ v5jwvpqs7uqgjmk0vfqd8kvjpwh8umawvi//4ccok=" />

    In order to protect the information stored in this field and in order to reduce its size, ASP. NET will perform special processing. When you load an ASP. NET page, all controls contain their default values, and soon after the default values are set, the runtime overrides these default values for the control with the value it finds in view state. When the page triggers a postback to modify the value of the control, ASP. The net runtime sees this change and stores the new value in view sate, so it is still available for subsequent postbacks. But not all controls are always dependent on view state, and there are many controls that maintain some of their own states. such as TextBox, CheckBox, RadioButton, and DropDownList.

  4. Turn off the state engine

    Because the view state engine adds a significant amount of information to the page, it is best to close it when it is not needed, which minimizes the size of the hidden _viewstate field, which means that the page becomes smaller and therefore faster to load in the browser.

    1. At the Web site level about the view state
      <system.web>    <pages enableviewstate="false">    </pages> </ System.web>
    2. at page level
       <%@ page language= " c#   autoeventwireup="  true   enableviewstate="  false   codebehind="  controlwebform.aspx.cs   inherits="   control learning. Asp. NET service control. Controlwebform  %> 
    3. At the control level
      <asp:label id="datelabel" runat="server" EnableViewState ="false" />
    4. Once you close the view state on a higher level, you can no longer open this feature in a lower tier, and then use the new ViewStateMode property to do the following:
      1. Disables the view state from being closed in the Web. config file.
      2. At the page level, set EnableViewState to True to set ViewStateMode to Disabled. This allows you to close the view state of all the controls on the page. Unless you explicitly set the ViewStateMode property to the enabled control again.

Asp. NET State engine

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.