Stateless Http
The root cause of stateless connection is that the browser uses Socket to communicate with the server. After the server returns the request result to the browser, the current Socket connection is closed. In addition, the server will destroy the page object after processing the page.
The reason for the application layer is that the communication between the browser and the server complies with the HTTP protocol.
The requests sent by a viewer are all responded to by the object implementing the IHttpHandler interface. Because the next access may not necessarily be the last response to the object, the object may have been destroyed after the last response is completed, the written class variable value has long existed, so the state information cannot be saved to the class variable.
Write an ashx
Copy codeThe Code is as follows:
Private int I;
Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
Context. Response. Write (I ++ );
}
Multiple refresh times, we found that the variable does not remember the last value.
Impact on websites: If a user enters some information, the data will be lost when the user jumps to the next page, and the data will no longer be obtained.
If you want to know the last status information, you have to record the status information somewhere:
A. Server Session
B. browser cookies
C. In the form Element-for example, hide the field <input type = "hidden"/> (Http packet) ViewState
Four important
ViewState:
ASP. NET. aspx page exclusive,Page-levelOf;
YesOn the pageHide the Save client in the domainA separate data method;
The values of the control on the server are automatically saved in ViewState;
Cookie:
This method enables the server or script to maintain the status information on the client;
YesOn the clientSave ClientA separate data method;
Just like your medical records, the hospital will bring you home directly;
Session: Related to the auxiliary process provided by. Net.
On the server sideSave ClientA separate data method;
Just like a bank account where all the money is stored in a bank, you can take a bank card [so-called SessionId] To Go Home (written into the Cookie of the client );
Application:
On the server sideSaveOne way to share data;
Just like the bank's single-person public toilet, anyone can go in, but once they go in, they will lock the door and then open the lock;
ViewState (page level)
Usage: Scope-page-level
Data storage method:
Copy codeThe Code is as follows:
ViewState ["myKey"] = "MyData ";
Data Reading method:
Copy codeThe Code is as follows:
String myData;
If (ViewState ["myKey"]! = Null)
{
MyData = (string) ViewState ["myKey"];
}
ViewState cannot store all data types. It only supports:
String, Integer, Boolean, Array, ArrayList, Hashtable
Prerequisites for using ViewState:
The page must have a server-side form tag (<form runat = "server">)
After receiving a user request for a page, the server will automatically find the hidden domain containing _ VIEWSTATE in the request message. If yes, decode the Intermediate Value and add it to the ViewState attribute of the page.
The server automatically adds the value in ViewState to the hidden field named _ VIEWSTATE in the form when outputting the ViewState.
VIEWSTATE is applicable to multiple interactions with the server without closing a page.
The _ VIEWSTATE submitted across pages will not be loaded into the ViewState attribute of the page by the target page.
Add runat = server to generate the html page Source Code as follows:
Not added as follows:
_ Principle of hiding a field in VIEWSTATE
Copy codeThe Code is as follows:
// When the page object executes the PR method, it first creates a control tree and then executes the loadState method to deserialize the value of _ VIEWSTATE in the request message. after being restored to a set, the key-Value Pair [ViewState automatically adds the runat = server control attributes and status on the page] That belongs to the programmer himself added to ViewStatue will be restored to the ViewState attribute of the page object. the principle of Page_Load is shown in the figure below.
Protected void Page_Load (object sender, EventArgs e ){
// The ViewState attribute of the page is actually the value of a hidden field named _ VIEWSTATE submitted by the browser.
If (ViewState ["name"]! = Null ){
String strName = ViewState ["name"]. ToString ();
Response. Write ("ViewState ['name']" + strName );
} Else {
// Add a key-value pair to ViewState
// ViewState. Add ("name", ""); same as the method below
ViewState ["name"] = ""; // Add a key-value pair to the hidden domain. If the key-value pair is not submitted to the server, ViewState ["name"] is always null.
ViewState ["name2"] = "Taobao sub-hair ";
}
}
The above code principle diagram:
Supplement:
Disable ViewState. enableviewstate = false is set for ViewState of a single control. Disable the entire Page. Add EnableViewState = "false" in the aspx Page command area ". ViewState can be used in the Intranet system and Internet backend. However, the Internet front-end should not be used. [Note: disabling does not save the server control attributes and values, but the hidden domain still exists]
The internal implementation of IsPostBack in WebForm is to determine whether the parameter _ ViewState is included in the page or get parameter and return the bool value.
Confirm:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e ){
If (IsPostBack) // return
Response. write ("as long as the request message submitted by the Browser contains the _ VIEWSTATE key"); // The address http: // localhost: 7148/ViewSatate on this page. add at the end of aspx? _ VIEWSTATE will output this code segment
Else
Response. Write ("ASP. NET will set the IsPostBack attribute of the page to true ");
}