ASP.Net ViewState implementation tutorial and code

  

ViewState is a new way of state preservation proposed in .Net (actually old bottled new wine); we know that traditional Web programs save state in such a way Kind: 1. Application This is the global save area in the lifetime of the web application. The data stored in the Application is globally valid. In Asp.Net, there is an application pool in which several (or dozens) are saved. An application instance, each request will take an instance from the pool to process the request, the instance will not accept other requests until the request is completed; this presents a problem, there may be multiple applications at the same time, that is, more Threads, these threads have the possibility to access the Application, so you need to consider the thread synchronization when processing the objects in the Application; in fact, the Application object internally implements a thread lock, calling its own Add, Remove, etc. Locking and unlocking operations are automatically invoked, but for performance reasons, for direct access The indexer or other way to get the object and operate on it, Application does not automatically handle thread synchronization, you need to use the following similar code to deal with: Application.Lock (); ((int)Application["Count"]) ++;Application.Unlock(); It is worth noting that after calling Lock, if there is no call to display Unlock, then at the end of this request, the Application object will be automatically unlocked, thus preventing the problem of deadlock, but For the robustness of the code, the Unlock method should be called immediately after the lock is called and the modification is completed. The Application object is essentially a Hash table. The object is stored according to the key value. Since the object is global and stored in the server, and there are multiple threads accessing at the same time, the Application should store more accesses, less modifications, and global. Data that at most most functions will use, such as counters or database connection strings. 2. Session Inside Asp.Net, there is a StateApplication to manage the Session. It is actually a helper process that handles the special request that the Session expires and is created. When each request is received, the Auxiliary Process will call the Status Server ( You can get a different state server through Web.config to get the Session. If there is no Session corresponding to the SessionId, it will create a new one and then bind to the context (HttpContext). Unlike Asp, how many Session Servers are there? Currently, there are three implementations inside Asp.Net: 1) InProcStateClientManager This is the traditional way to save Session, but there are still some nuances. 2) SqlStateClientManager This is the way to save the session to the database. 3) OutOfProcStateClientManager This is to save the Session to the process. The external way Asp.Net's Session mechanism has a feature that the auxiliary process that handles the Session is separate from the state server that holds the Session. According to MSDN, the following benefits are: “Because the memory used for session state is not in ASP. NET auxiliary process, so can Achieve recovery from application failures. & rdquo;  Because all states are not stored with the worker process, you can cleanly partition the application across multiple processes. This kind of partitioning can significantly improve the usability and scalability of applications on computers with multiple processes. & rdquo;  Because all states are not stored with the worker process, you can partition the application across multiple worker processes running on multiple computers. ”Asp.Net's Session mechanism personal point of view, feels better flexibility, internal implementation is also clever, but in fact because there is not too much testing, so the application will not be as good as it said, afraid to pack ticket. I have the opportunity to write a separate article to explore the Session mechanism inside Asp.Net.

3, Cookie This is nothing to say, in fact, Asp.Net and Asp's cookies are no different, perhaps this technology is mixed, and more dependent on the client implementation, MS has no improvement.

4, ViewState This is our focus today; in fact, ViewState is not mysterious, is a Hidden field, but it is the basis for the state preservation of server controls; unfamiliar friends can use IE to view Html source code, Find a Hidden field called "__VIEWSTATE", which has a bunch of messy characters, which is the ViewState of the page.

People who have done web applications may have this painful experience. Sometimes, in order to deal with the more complicated functions on the page, Hidden is often added, and then a lot of judgments are used on the server side to analyze the current The state is annoying to write, and it is ugly to write the code; in fact, ViewState is a function that saves the state of the control for our system. The server-side control can save state in multiple requests. Ok, the introduction is here. Today we are not talking about the use of ViewState, but exploring the essence of this thing from the inside. We first build a test page: <%@ Page language="c#" Codebehind="ViewStateTest.aspx.cs" AutoEventWireup="false" Inherits="CsdnTest.ViewStateTest" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><html><head><title>ViewStateTest</title><meta name="GENERATOR" Content=" Microsoft Visual Studio 7.0"><meta name="CODE_LANGUAGE" Content="C#"><meta name="vs_defaultClientScript" content="JavaScript"><meta name="vs_targetSchema" ; content="http://schemas.microsoft.com/intellisense/ie5"></head><body><form id="ViewStateTest" method="post" runat="server" ;><asp:Button ID="btnPostBack" Runat="server" Text="Post Back" Width="85px"></asp:Button><br/>< Asp:CheckBox ID="chkTest" Ru Nat="server" Text="This is a check box"></asp:CheckBox></form></body></html>

This is with Vs A simple page designed by .Net that contains a server-side button and a CheckBox, and then we respond to button events on the server side:

private void btnPostBack_Click(object sender, System.EventArgs e){ [1] Response.Write( "ViewState :"+Request.Params["__VIEWSTATE"]+"<br/>" );[2] string decodeValue = Encoding.UTF8.GetString( Convert. FromBase64String( Request.Params["__VIEWSTATE"] ) );

Copyright © Windows knowledge All Rights Reserved