A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
This article will discuss several methods for transferring data between ASP. NET pages, hoping to help you understand the usefulness and convenience of transferring data between ASP. NET pages.
The web page is stateless, and the server considers each request to come from different users. Therefore, the status of the variable is not retained between consecutive requests to the same page or when the page jumps. ASP. when designing and developing a web system, you may encounter an important problem: how to ensure that data is transmitted correctly, securely, and efficiently between pages, asp.net provides multiple technologies, such as status management, to save and transmit data.. net.
1. Methods and analysis of data transmission between ASP. NET pages
1.1 Use the querystring Method
Querystring is also called a query string. The data to be transmitted in this method is appended to the webpage address (URL) for transmission. For example, when page A. aspx jumps to page B. aspx, you can use request. Redirect ("B. aspx? Parameter Name = parameter value ") method, you can also use a hyperlink:, after the page jumps, you can use ruquest [" parameter name "] on the target page to receive parameters. The querysting method is easy to implement and does not use server resources. The disadvantage is that the passed value is displayed on the address bar of the browser, which may cause tampering and cannot pass objects, it is feasible to query strings only when the URL request page is passed.
1.2 use hidden Domains
Hidden fields are not 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 webpage-related information stored on a webpage. When using a hidden field to store values, use the: hidden control. value = value. When getting the received value, use the: Variable = hidden control. value. The advantage of hiding a domain is that it is easy to implement. Hiding a domain is a standard HTML control without complicated programming logic. Hidden domains are stored and read on pages without any server resources. Almost all browsers and client devices support forms with hidden domains. The disadvantage is that the storage structure is small, and only a simple data structure is supported. The storage capacity is small because it is stored on the page itself, so it cannot store large values, in addition, a large amount of data is blocked by firewalls and proxies.
Viewstate is a hidden form field managed by the ASP. NET page framework. When ASP. NET executes a page, the viewstate value and all controls on the page are collected and formatted into an encoding string, and then assigned to the value attribute of the hidden form field. When you use viewstate to pass data, you can use viewstate ["variable name"] = value. When you retrieve data, use viewstate ["variable name"]. Viewstate has the following advantages: the value is automatically retained among multiple requests on the same page, without the need for server resources. This is simple, and the values in the view State are hashed and compressed, in addition, for Unicode implementation encoding, its security is higher than the use of hidden fields; disadvantage is that viewstate is stored on the page itself, if
The storage of large values may slow down when users display pages and send pages. Although the view State stores data in a hash format, it can still be tampered.
1.5 Use the application variable
You can use the application variable to transfer values between pages. The application variable is global and all users share an application variable. Once defined, it will affectProgram. It is the best choice to use a variable value application object throughout the application. When saving the data, add the value to the application variable: application ["variable name"] = value; Use the variable = application ["variable name"] to retrieve the data. when you do not need to use this application, you need to explicitly clear it: application ["volume name"] = NULL.
Application Advantages: easy to use and global scope. All pages in the program are available for access. Disadvantage: if the server-side process that stores data is damaged (for example, the server crashes, upgrades, or shuts down), the data will be lost, so the application must have a minimum protection policy; this may affect the server performance and scalability of applications.
1.6 use session Variables
The Session object can be used to store the information of the specified conversation to be maintained. Different clients generate different session objects. Sessions are used to store short-term information specific to individual sessions. The usage and format of the session are the same as those of the application.
Advantages: it is easy to implement and provides high security and durability. It can be used in multi-process scenarios to cope with IIS restart and secondary process restart. The disadvantage is that the server memory is consumed. Therefore, do not store a large amount of information. The most common purpose of session is to provide the user identification function to Web applications together with cookies. sessions can also be used in browsers that do not support cookies. However, when using a session without a cookie, you need to place the session identifier in the query string, which also meets the security issues described in the query string section.
1.7 use static attributes of a class
This method uses static attributes of the class to implement value transfer between two pages. Defines a class containing static attributes. values to be transferred are assigned to static attributes. values to be transmitted on the source page can be obtained through static attributes on the target page.
The advantage is that it can easily transmit multiple data. The disadvantage is that additional programming is required to increase the workload of programming and occupy the server memory.
1.8 use server. Transfer
Through server. the transfer method transfers the execution flow from the current aspx file to another ASPX page on the same server, while retaining form data or querying strings, set the second parameter of this method to true, and use server on the first page. transfer ("target Page name. aspx ", true); used to retrieve data from the target page: ruquest. form ["Control name"] Or ruquest. querystring ["Control name"]. ASP. net2.0 can also be used in this way,CodeAs follows:
PG1 = (previouspage) Context. Handler;
Response. Write (pg1.name );
Note: This code is used to retrieve the passed value from the target page. Previous-page is the class name of the original page, and name is the attribute defined on the original page, the data to be transferred is stored in this attribute.
To use this method, you need to write some code to create some attributes so that you can access it on another page. You can access the value on another page in the form of object attributes, this method is particularly useful in inter-page value transfer. This method is not only concise, but also object-oriented.
Cache has powerful data operations. It stores data in the form of a set of key-value pairs. You can specify keywords to insert and retrieve data items. Its Dependency-based termination function enables it to precisely control how to update and eliminate slow data in a timely manner. It can implement internal Lock Management and does not need to use the lock () and unlock () methods as the application object for serial management. The disadvantage is that the use method is complicated, and improper use reduces the performance.
2. The value passing method can be used for different page jumps
Case 1: 2.1: The Source Page can jump to the target page, the Source Page passes data to the target page
It is a simple and common method to transmit a small amount of information from one page to another using a query string and avoid security issues. the transfer method can pass form data or query strings to another page, and 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.
Case 2: 2.2: Pass a value to the page
That is, the value is retained between multiple requests on the same page. The viewstate attribute provides basic security functions. It can also be used to store a small amount of page information sent back to itself or another page, without considering security issues.
Case 3: 2.3: The value of the Source Page is passed to the target page, and The Source Page cannot be directly connected to the target page.
There are multiple methods. The specific use depends on the specific situation.
Application: stores global information that is used by multiple users and is not frequently changed. Security is not a problem at this time. Do not store a large amount of information. Session: stores short-term information specific to individual sessions, and requires high security. Do not store a large amount of information in the session state. Note that the session state object will be created and maintained for the lifetime of each session in the application. In applications that support many users, this may occupy a large amount of server resources and affect scalability.
COOKIE: It is used when you need to store a small amount of information on the client and there is no security problem. Class to facilitate the transmission of multiple data.
Cache: objects are used by a single user, a group of users, or all users. Data can be stored for multiple requests for a long time and efficiently. The above methods are not only used in case 3, but can be used in both cases, but are used as little as possible if there is no need, otherwise it will cause a waste of resources or increase the complexity of the program.
Start building with 50+ products and up to 12 months usage for Elastic Compute Service