In this article, we will discuss several methods of data transfer between ASP.NET pages. We hope this can help everyone correctly understand the usefulness and convenience of data transfer between ASP.NET pages.
0. Introduction
Web pages are stateless. The server considers each request to be from a different user. Therefore, the state of variables will not be retained between multiple consecutive requests for the same page or when the page jumps. When designing and developing a Web system with ASP.NET, an important problem encountered is how to ensure that data is transmitted correctly, safely and efficiently between pages. Asp.net provides a variety of technologies such as state management to solve the problem of preservation and transmission. Regarding data issues, let’s explore the various methods to solve this problem under .NET and their respective applicable occasions.
1. Various methods and analysis of data transfer between ASP.NET pages
1.1 Using Querystring method
QueryString is also called query string. This method appends the data to be transferred to the end of the web page address (URL) and transfers it. For example, to jump from page A.aspx to page B.aspx, you can use the Request.Redirect("B.aspx? Parameter name = parameter value") method, or you can use the hyperlink:, after the page jumps, it will be available in the target page. Ruquest["parameter name"] to receive parameters. The advantage of using the QuerySting method is that it is simple to implement and does not use server resources; the disadvantage is that the passed value will be displayed on the browser's address bar, with the risk of being tampered with, and the object cannot be passed. The query string can only be used when requesting the page through the URL. is feasible.
1.2 Utilizing hidden fields
Hidden fields will not be displayed in the user's browser. Generally, a hidden control is added to the page. When interacting with the server, the value is assigned to the hidden control and submitted to the next page. A hidden domain can be a repository of any information stored in a web page that is relevant to the web page. When using a hidden field to store a value, use: hidden control.value=numeric value. When taking out a received value, use: variable=hidden control.value. The advantage of using hidden fields is that they are simple to implement. Hidden fields are standard HTML controls and do not require complex programming logic. Hidden fields are stored and read on the page, do not require any server resources, and nearly all browsers and client devices support forms with hidden fields. The disadvantage is that it has few storage structures and only supports simple data structures. The storage capacity is small because it is stored in the page itself, so it cannot store larger values, and large amounts of data will be blocked by firewalls and proxies.
1.3 ViewState
ViewState is a hidden form field managed by the ASP.NET page framework. When ASP.NET executes a page, the ViewState values and all controls on the page are collected and formatted into an encoded string, and then assigned to the Value property of the hidden form field. When using ViewState to transfer data, you can use: ViewState ["variable name"]=numeric value. When retrieving data, use: variable=ViewState["variable name"]. The advantages of using ViewState are: values are automatically retained between multiple requests for the same page, no server-side resources are needed, and the implementation is simple. The values in the view state are hashed and compressed, and are encoded for Unicode implementation, and their security requires Higher than using a hidden field; the disadvantage is that because ViewState is stored in the page itself, if a larger value is stored, the user may be slowed down when displaying the page and sending the page. Although view state stores data in hashed format, it can still be tampered with.
1.4 Use of cookies
Cookies can transfer a small amount of information between pages and can be stored in a text file on the client or in the client's memory. The Cookie method is suitable for storing information that changes frequently in a small amount of pages, such as saving login usernames for logged-in websites, providing convenience for user input, and saving users' personalized settings on some user-defined items. Available when using cookies to transfer data: Response.Cookies["key name"]=key value; to retrieve data: variable name=Request.Cookies["key name"]. The advantages of using cookies are: Cookies are stored on the client, do not use server resources, are simple to implement, and can configure the expiration time. Disadvantages: The amount of data that can be stored is relatively small. Since cookies are not supported by all browsers and may be banned or deleted by users, they cannot be used to save critical data. In addition, cookies are stored in simple plain text, and it is not appropriate to store sensitive, unencrypted data in them.
1.5 Using Application variables
Application variables can also be used to transfer values between pages. Application variables are global and all users share an Application variable. Once defined, it will affect all parts of the program. If you want to use a certain variable value across the entire application, the Application object would be the best choice. When storing data, add the value to the Application variable: Application["variable name"]=numeric value; use to retrieve data: variable=Application["variable name"]; when you do not need to use the Application, clear it explicitly :Application["quantity name"]=null.
Application Advantages: Easy to use, global scope. Available to all pages in the application. Disadvantages: If the server-side process that saves data is damaged (such as due to server crash, upgrade or shutdown), then the data will be lost, so you must have a guaranteed strategy when using Application; it occupies server-side memory, which may affect the server performance and application scalability.
1.6 Using Session variables
Session objects can be used to store information about specified conversations that need to be maintained. Different clients generate different Session objects. Session is used to store short-term information specific to an individual session. The usage and format of Session are the same as Application.
Advantages: It is easy to implement, provides high security and durability, can cope with IIS restart and auxiliary process restart, and can be used in multiple processes. The disadvantage is that it consumes server-side memory. So don't store a lot of information. The most common use of Session is to provide user identification functions to Web applications together with Cookies. Sessions can also be used in browsers that do not support Cookies. However, using a cookieless Session requires placing the session identifier in the query string, which also suffers from the security issues stated in the query string section of this article.
1.7 Using static properties of classes
This method uses the static properties of the class to transfer values between the two pages. Define a class containing static attributes; assign the value to be transferred to the static attribute; the target page can obtain the value to be transferred in the source page through the static attribute.
The advantage is that it can easily transmit multiple data, but the disadvantage is that it requires additional programming, increases the workload of programming, and takes up server memory.
1.8 Using Server.Transfer
While transferring the execution flow from the current ASPX file to another ASPX page on the same server through the Server.Transfer method, the form data or query string can be retained. The method is to set the second parameter of the method to True. The first page uses Server.Transfer("target page name.aspx", true); the target page uses: Ruquest.Form["Control Name"] or Ruquest.QueryString["Control Name"] to retrieve data. It can also be used in Asp.net2.0 like this, the code is as follows:
PreviousPage pg1;
pg1=(PreviousPage)Context.Handler;
Response.Write(pg1.Name);
Explanation: This code is used to retrieve the passed value from the target page. Previous-Page is the class name of the original page, Name is the attribute defined on the original page, and the data that needs to be transferred is stored in this attribute.
Using this method, you need to write some code to create some properties so that you can access it from another page. You can access the value as an object property in another page. This method is particularly useful in passing values between pages. This method is not only simple, but also object-oriented.
1.9 Cache
Cache has powerful data operation functions. It stores data in the form of a collection of key-value pairs. Data items can be inserted and retrieved by specifying keywords. Its dependency-based termination capabilities allow it to precisely control how data in the cache is updated and eliminated in a timely manner. It can perform lock management internally and does not need to use the Lock() and Unlock() methods for serialization management like the Application object. The disadvantage is that the method of use is complicated, and improper use will reduce performance.
2. Value transfer methods that can be used in different page jump situations
2.1 Scenario 1: The source page can jump to the target page, and the source page passes data to the target page.
Using query strings is a simple and commonly used method to transfer a small amount of information from one page to another and there are no security issues; use the Server.Transfer method to pass form data or query strings to another page. You can also save the HttpContext of the initial page. This method can be used when the target page and the source page are on the same server.
2.2 Case 2: The page passes the value to its own page
That is, by retaining values across multiple requests for the same page, the ViewState property provides functionality with basic security. Hidden fields can also be used to store a small amount of page information that is posted back to itself or another page, and are used when security issues are not considered.
2.3 Case 3: The source page passes values to the target page, but the source page cannot be directly connected to the target page.
There are multiple methods, and which one to use depends on the specific situation.
Application: Stores global information that is used by multiple users and changes infrequently, when security is not an issue. Don't store large amounts of information. Session: Stores short-term information that is specific to an individual session and requires high security. Don't store large amounts of information in session state. Note that session state objects are created and maintained for the lifetime of each session in the application. In applications that support many users, this can consume significant server resources and impact scalability.
Cookies: Used when you need to store a small amount of information on the client side and there are no security issues. Static attributes of the class facilitate the transmission of multiple data.
Cache: object for a single user, a group of users, or all users. Data can be saved for multiple requests for a long time and efficiently. The above methods are not only used in case three, but also in the previous two cases. Just use them as little as possible when unnecessary, otherwise it will cause a waste of resources or increase the complexity of the program.