Describes how to transfer data between ASP. NET pages in detail.

Source: Internet
Author: User
Tags server memory

InASP. NETBetween pagesData TransmissionThere are many methods. The following is a summary of the data transfer methods between 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.

I. Methods and analysis of data transmission between ASP. NET pages

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.

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, only simple data structures are supported, and the storage capacity is small. Because it is stored on the page itself, it cannot store large values, in addition, a large amount of data is blocked by firewalls and proxies.

3. viewstate

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. The disadvantage is that viewstate is stored on the page itself. Therefore, if a large value is stored, page display and page sending may slow down. Although the view State stores data in a hash format, it can still be tampered.

4. Use cookies

Cookies can transmit a small amount of information between pages. They can be stored in client text files or in client memory. The cookie method is suitable for storing a small amount of information that is frequently modified on the page. For example, it is convenient to save the login user name for a logged-in website, you can also save your personalized settings on custom projects. When using cookies to transmit data, you can use: Response. Cookies ["key name"]. value = parameter; When retrieving data, use: variable name = request. Cookies ["key name"].

The advantage of using cookies is that the cookie is stored on the client and does not use server resources. It is easy to implement and can be configured with the expiration time. The disadvantage is that the data volume that can be stored is small. Because cookies are not supported by all browsers and may be disabled or deleted by users, they cannot be used to store key data. In addition, cookies are stored in plain text, which cannot store sensitive and unencrypted text.
Data.

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 storing data,
Add value to Application
In the variable: application ["variable name"] = value; used to retrieve data: Variable = application ["variable name"]; this application is not required
To explicitly clear it: application ["volume name"] = NULL.

Application
Advantages: easy to use and global. All pages in the program are available for access. Disadvantage: if the server-side process that stores data is damaged (for example, due to server crash, upgrade, or shutdown), the data will be lost.
Therefore, the application must have a guaranteed-Performance Policy. It occupies the server memory, which may affect the server performance and scalability of applications.

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 to cope with IIS
Restart and secondary process restart, which can be used in multiple processes. The disadvantage is that the server memory is consumed. Therefore, do not store a large amount of information. Session is most commonly used with cookies.
Together, the user identification function is provided to Web applications. sessions can also be used in browsers that do not support cookies. However, use a session without a cookie
You need to place the session identifier in the query string, and you will also encounter the security issues described in the query string section.

7. Use static attributes of the 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.

8. Use server. Transfer

Use the server. Transfer Method to transfer the execution process from the current aspx file to another aspx on the same server
The form data or query string can be retained at the same time. The second parameter of this method is set to true, and server. Transfer ("target page" is used on the first page.
Name. aspx ", true); used to retrieve data from the target page: ruquest. Form [" Control name "] Or ruquest. querystring [" Control name"
It is called "]. ASP. net2.0 can also be used in this way,CodeAs follows:

1Previouspage PG1;
2PG1=(Previouspage) Context. Handler;
3 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.

9. Cache

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

Scenario 1: The Source Page jumps to the target page, and 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.

Scenario 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: The Source Page passes the value 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.

We hope that you will have a better understanding of the data transfer methods between pages through the content above. Hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.