Built-in object method information size scope and application scope of save time
=============================================================================
Application data of any size the entire application's lifetime entire application/all users
Session a small amount of simple data user activity time + latency (default 20 minutes) individual user
Cookies a small amount of simple data can be set to a single user
Viewstate a small amount of simple data a single user of a Web page's lifetime
Cache data of any size can be set to the entire application/all users as needed
Hide a small amount of simple data a single user for the lifetime of a Web page
Query string A small amount of simple data until the next page Jump request service to a single user
========================================================================================
1.Application:Used to hold data information shared by all users. Similar configuration data in asp.net is best saved in Web.config files. If you use the Application object, one of the issues to consider is that any write operation is done in the Application_OnStart event (Global.asax). Although the Application.Lock and Application.UnLock methods are used to avoid the synchronization of write operations, they serialize the request of the Application object, which can cause serious performance bottlenecks when the site is heavily visited. Therefore, it is best not to save large datasets with this object.
Use the following:
Store information
application["Usernameid"]= "1000";
Application.add ("USer", DataTable);
Read information
String nameid=application["Usernameid"]. ToString ();
Gridview1.datasource = application["USer" as DataTable;
Gridview1.databind ();
2.Session:Used to hold private information for each user. The information in the session is stored in the Web server's memory, and the amount of data saved can be large and small. Saved data information is automatically released when the session times out or is closed. It is a good choice to save a small amount of data session object.
Use the following:
Store information
session["Usernameid"]= "1000";
Read information
String nameid=session["Usernameid"]. ToString ();
3.Cookie:The request information that is used to hold the client browser requesting the server page is valid for human settings and the amount of data stored is limited, so do not save the dataset and other large amounts of data. and cookies store data information in plaintext on the client computer, so it is best not to save sensitive unencrypted data.
Use the following:
Store information
response.cookie["Usernameid"]. value= "1000";
Read information
String nameid=response.cookie["Usernameid"]. Value;
4.ViewState:Often used to hold the state information of a single user, you can save a large amount of data, but too much use can affect the performance of your application. All Web server controls use Viewstat to save their state information during page postbacks. Each control has its own viewstate, which is best closed to conserve resources. You can disallow the viewstate of an entire page by adding the "enableviewstate= false" attribute to the @page directive.
Use the following:
Access information
viewstate["Usernameid"]= "1000";
Read information
String nameid=viewstate["Usernameid"]. ToString ();
5.Cache:Used to save pages and data between HTTP requests. It allows frequent access to a large number of server resources in memory, when the user issued the same request, the server will not be processed, but the information stored in the cache returned to the user, saving the server processing request time.
Use the following:
Store information
cache["Usernameid"]= "1000";
Store information
Cache.Insert ("Usernameid", "1000");
Read information
String usernameid=cache["Usernameid"]. ToString ();
6. Hidden fields:The hidden control, which is a server control of HTML type, can realize the function of a hidden field, and he is no different from any other space except that it is not displayed on the browser and is always hidden.
Use the following:
Store information
hidden.value= "1000";
Read information
String Usernameid=hidden.value;
7. Query string:Connect the passed value to the URL, and then implement the client redirection through the Response.Redirect method.
Use the following:
Response.Redirect ("User.aspx?") Usernameid=10000&leveld=100 ");
The code for the URL displayed in the IE Address bar after executing the above statement is as follows:
http://localhost/User.aspx?UserNameID=1000&LevelID=100
When you jump to user.aspx, you can get the information you pass by using the following code:
String Usernameid,leveld;
usernameid=request.params["Usernameid"];
leveld=request["Leveld"];
========================================================================================
Application
1, application used to save all the information shared by users
2. In the ASP era, using application is an ideal choice if the data to be saved is not or rarely changed during the lifetime of the application. But in the asp.net development environment we put similar configuration data in Web.config.
3, if you want to use application to note that all writes must be done in the Application_OnStart event (Global.asax), although you can use Application.Lock () to avoid conflicts, However, it has serialized requests for application, resulting in serious performance bottlenecks.
4, do not use application to save large amount of data information
5, Code: application["UserID"]= "test";
String username=application["UserID"]. ToString ();
Session
1, session to save each user's proprietary information
2, the lifetime of the session is the user's continuous request time plus a period of time (usually about 20 minutes)
3, session information is stored in the Web server memory, save the amount of data can be large and small
4, session timeout or shutdown will automatically release data information
5. This method is less efficient because it remains in memory for some time after the user stops using the application
6, Code: session["UserID"]= "test";
String username=session["UserID"]. ToString ();
Cookies
1, cookies to save the client browser Request Server Page request information
2, we can store sensitive user information, save time can be set according to need
3. If no cookie expiration date is set, its lifecycle is saved until the browser is closed
4. The Expires property of the cookie object is set to MinValue, which means never expiring
5. The amount of data stored by cookies is limited, and most browsers are 4K so do not store large data
6, because not all browsers support cookies, data will be stored in the form of clear text in the client
7, Code: resopnse.cookies["UserID"]= "test";
String username= resopnse.cookies ["UserID"]. ToString ();
ViewState
1, viewstate is used to save the user's state information, the validity period is equal to the page life cycle
2, can save a lot of data but use caution, because it will affect program performance
3. All Web server controls are saved with ViewState during page postback
4, do not need to close the @page inside set Enableviewstate=false
5, Code: viewstate["ID"]= "CC";
String ID =viewstate["id"]. ToString ();
Cache
1. Cache is used to save pages or data during HTTP requests
2, the use of cache can greatly improve the efficiency of the entire application
3, it allows frequent access to the server resources stored in memory, when the user issued the same request, the server is not processed again but the cache saved data directly returned to the user
4, you can see that the cache is saving time-server processing time
5, the cache instance is each application exclusive, its life cycle = = The application cycle
Application restart will re-create its instance
6. Note: If you want to use features such as cache cleanup, expiration management, dependencies, and so on, you must add information by using the Insert or Add method method
7, code: cache["id"]= "CC", or Cache.Insert ("id", "test");
String ID =cache["id"]. ToString ();
Hidden
1, the hidden control is an HTML type of server control, always in a hidden state
2, each time it is submitted to the server side with the other server control
3, the code is as follows: Hidden.value= "CC";
String Id=hidden.value; To use Runat=server