Asp. The method of data transfer between NET pages

Source: Internet
Author: User
Tags server memory

In this paper, we will discuss several methods of data transfer between ASP, and hope to help you understand the usefulness and convenience of data transfer between ASP.

Introduction

Web pages are stateless, and the server considers each request to come from a different user, so the state of the variable is not persisted between multiple requests for the same page consecutively or on the page. When developing a Web system with an ASP. NET design, an important question is how to ensure that data is transferred correctly, securely and efficiently between pages, and that ASP. NET provides state management and other technologies to solve the problem of saving and transmitting data. NET to solve this problem in various ways and for each applicable occasion.

Asp. The various methods and analysis of data transfer between NET pages

1. Using the QueryString method

QueryString is also called a query string, which appends the data to the Web address (URL) to be passed. such as page a.aspx jump to page b.aspx, you can use the Request.redirect ("b.aspx? parameter name = parameter value") method, you can also use hyperlinks:, after the page jumps, in the target page to use ruquest["parameter name" to receive parameters. The advantage of using the Querysting method is that the implementation is simple and does not use server resources; The disadvantage is that the passed value is displayed on the address bar of the browser, there is a risk of tampering, the object cannot be passed, and the query string is only feasible if the page is requested via a URL.

2. Using hidden fields

Hidden fields are not displayed in the user's browser, typically by adding a hidden control to the page, assigning the value to the hidden control when interacting with the server, and submitting it to the next page. A hidden field can be any repository of information about a Web page that is stored in a Web page. Use hidden fields when storing values: hidden controls. Value= the value, remove the value when it is received: variable =hidden control. Value The advantage of using hidden fields is that they are simple to implement, that hidden fields are standard HTML controls and do not require complex programming logic. Hidden domains are stored and read on the page, do not require any server resources, and almost all browsers and client devices support forms with hidden fields. The disadvantage is that the storage structure is small, only support simple data structure, storage is small, because it is stored in the page itself, so large values can not be stored, and the large amount of data will be blocked by firewalls and agents.

3.ViewState

ViewState is a hidden form field that is managed by the ASP. When ASP. NET executes a page, the ViewState value and all controls on that page are collected and formatted into an encoded string, which is then assigned to the Value property of the hidden form field. Available when passing data using ViewState: ViewState ["Variable name"]= value, used when extracting data: variable =viewstate["variable name"]. The advantage of using ViewState is that the values are automatically persisted across multiple requests to the same page, that the values in the view state are hashed and compressed without server-side resources, and that encoding for the Unicode implementation is more secure than using hidden fields; The disadvantage is that viewstate stored in the page itself, so if

Stores large values that may slow down when the user displays the page and sends the page. Although view state stores data in a hashed format style, it can still be tampered with.

4. Use of cookies

Cookies can pass a small amount of information between pages, can be stored in the client's text file, and can be stored in the client's memory. The Cookie method is useful for storing frequently changed information on a small number of pages, such as saving a login user name for a logged-in website, facilitating user input, and saving the user's personalization on some user-defined items. Available when using cookies to pass data: response.cookies["key Name"]= key value; Remove data by: variable name =request.cookies["key Name"]. The advantage of using cookies is that cookies are stored on the client, do not use server resources, implement simple, configurable expiry times. The disadvantage is that the amount of data that can be stored is less, because cookies are not supported by all browsers and may be banned or deleted by the user, so they cannot be used to save critical data. In addition, the Cookie is saved in the form of simple plaintext text, which is not suitable for storing sensitive, unencrypted data.

5. Using the Application variable

The application variable can also be used to achieve the transfer of values between pages, application variables are global, all users share a application variable, once defined, it will affect all parts of the program. If you want to use a variable value for the entire application scope Application object will be the best choice. When the data is stored, add the value to the application variable: application["variable name"]= value, take out the data by: variable =application["variable name"]; When you do not need to use the application, To explicitly clear it: application["volume name"]=null.

Application advantages: Easy to use, global scope. Accessible to all pages in the application. Cons: If the server-side process that holds the data is corrupted (such as corruption due to server crashes, upgrades, or shutdowns), then the data is lost, so the use of application must have a guaranteed policy, occupying server-side memory, which can affect the performance of the server and the scalability of the application.

6. Use the session variable

Session objects can be used to store information about the specified conversation that needs to be maintained, and different clients generate different session objects. Session is used to store short-term information specific to a separate session. The Session is used in the same way and format as application.

Pros: Easy to implement and provide high security and durability, can be used in multiple processes to handle IIS restart and worker process restarts. The disadvantage is that it consumes server-side memory. So don't store a lot of information. The most common use of a session is to provide a user identity function with a cookie to a Web application, and the session can also be used for browsers that do not support cookies. However, using a session without a cookie requires that you place the conversation identifier in the query string, as well as the security issues that are stated in the query string section of this article.

7. Using static properties of a class

This method uses the static properties of the class to implement the value transfer between the two pages. Defines a class that contains a static property, assigns a value to a static property, and the target page can get the value to be passed in the source page through a static property.

The advantage is that you can easily transfer multiple data, the disadvantage is the need for additional programming, increase the workload of programming, Occupy server memory.

8. Using Server.Transfer

By using the Server.Transfer method to move the execution process from the current ASPX file to another ASPX page on the same server, you can preserve the form data or query string by setting the second parameter of the method to true. On the first page, use the Server.Transfer ("target page name". aspx ", true); The target page takes the data: ruquest.form[" control name "] or ruquest.querystring[" control name ". Asp.net2.0 can also be used in this way, the code is as follows:

PreviousPage PG1;

pg1= (previouspage) Context.Handler;

Response.Write (Pg1.name);

Note: This code is used to remove the value passed in the target page, Previous-page is the class name of the original page, name is the property defined on the original page, the data that needs to be passed into this property.

Using this method, you need to write some code to create some properties so that you can access it on another page, which can be accessed in another page as an object property, a method that is particularly useful in the transfer of values between pages, which is not only concise but also object-oriented.

9.Cache

The Cache has powerful data manipulation capabilities to store data in the form of key-value pairs, and you can insert and retrieve data items by specifying keywords. Its dependency-based termination feature enables it to precisely control how and when data in the cache is updated and eliminated. It can be managed internally, without the need for serialization management using the lock () and Unlock () methods, as application objects do. The disadvantage is that the use of the method is more complex, and improper use to reduce performance.

The method of transmitting value in the case of different page jump

1. Situation one: The source page can jump to the target page, the source page to pass the data to the target page

Using a query string to transfer a small amount of information from one page to another and without security issues is a simple and common method, using the Server.Transfer method, you can pass the form data or query string to another page, and you can 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. Scenario Two: page passing values to itself page

That is, the ViewState property provides functionality with basic security while preserving values between multiple requests on the same page. You can also use a hidden field to store a small amount of postback to itself or another page of page information when used without regard to security issues.

3. Scenario Three: The source page passes values to the target page, and the source page cannot be connected directly to the target page.

There are several ways to use which depends on the specific situation.

Application: Stores global information that is used by multiple users and changes infrequently, and security is not a problem at this time. Do not store large amounts of information. Session: Stores short-term information that is specific to a separate session and requires high security. Do not store large amounts of information in session state. It is important to 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 a lot of server resources and affect scalability.

Cookies: used when you need to store a small amount of information on the client and there is no security issue. A static property of the class that facilitates the transfer of multiple data.

Cache: Objects are used for a single user, a group of users, or all users. Data can be saved for a long time and efficiently for multiple requests. The above-mentioned methods, not only for the situation three, the previous two cases can be used, but not necessary to minimize the use, otherwise it will cause waste of resources or increase the complexity of the program.

Asp. The method of data transfer between NET pages

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.