Lists several methods for passing values between ASP. NET pages.
1. URL-based value transfer
This is a classic value transfer method, such as XXX. aspx? Id = 1 & name = c; however, the passed value is displayed in the address bar of the browser, and the object cannot be passed. Therefore, this method is generally used when the passed value is small and the security requirements are not high.
2. Pass the Session Value
This method stores each piece of data in server variables and can transmit a large amount of data, which is highly secure. Therefore, it is often used for user identity verification. However, if the Session variable stores too much data, it will consume too much server resources. programmers should exercise caution when using it. Sessions can be shared among multiple pages of the application by name/value pairs until the browser user closes his or her browser or the server Session times out (configurable, the default value is 20 minutes.
Session has the following features:
Session data is stored on the server;
The Session can store any type of data;
The default life cycle of a Session is 20 minutes. You can manually set a longer or shorter time.
3. Pass Cookie values
Cookie is a special data storage method, because it stores data in the browser's computer and exists in the disk as a text file. This method is very interesting. Many login systems use cookies to automatically log on to users. That is, the login information of a user login will be written into the Cookie file of the user's computer. The website will automatically read the Cookie for authentication at the next login. Although it is convenient to transmit data through cookies, the storage time can be set freely, but the security is not high, programmers should not rely too much on cookies, but should use a combination of methods to store sensitive data.
Cookie-based data storage has the following features:
The data in the Cookie is stored on the client;
Cookies can only store string-type data. If you need to save other types of data in cookies, you need to convert the data to string-type data and save it;
The Cookie also has its default life cycle. You can also manually set it to expire after 50 years at most.
4. Server. Transfer
This method has many steps. You can use this method to access values on another page by exposing object attributes. This method is object-oriented. The code of this method is not complex. First, you can define a public permission attribute to return the value to be passed. Then, on the second page, use the Context. Handler attribute to obtain the reference of the instance object on the previous page. You can access the custom attribute to obtain the required value.
5. Pass the Application value
Strictly speaking, the HTTP application object generates a status variable on the server to store the required information. The availability of the HttpApplication object variable covers the entire WEB application. Therefore, this object generally stores information to be published, such as the number of online users. This method is not used for sensitive data involving individual users. The HttpApplication object has two common methods: Lock and UnLock, which can be used to process data written by multiple users in the Application variable. The Lock method locks all Application variables to prevent other users from modifying the variable values of the Application object. The UnLock method unlocks the variables of the HttpApplication object. The method for passing values through the HttpApplication object is similar to that of the Session, but the Session is for each individual user. When the user closes the browser, the Session fails. The variables stored in HttpApplication object are for all users accessing the program. Even if a user closes the browser, the value of the variables will not be lost.
6. Cross-page Transfer
Cross-page Transfer is similar to the Transfer method that calls the HttpServerUtility object, but it is more efficient. Because the Transfer method that calls the HttpServerUtility object is a server-based method, and cross-page Transfer is a browser-based method. This method is mainly used to set the "PostBackUrl" attribute of the control, so that the control (such as Button) is switched to the specified page after the operation, in addition, this specified page can directly obtain all the control objects and their attribute values on the previous page.
7. If you have special requirements, you can also use other methods, such as storing temporary data through a database.
From Peter Luo