ASP. NET introduces several methods of data transfer between pages

  • 2020-06-07 04:23:26
  • OfStack

Web pages are stateless and the server considers each request to be from a different user, so the state of a variable is not retained between successive requests to the same page or when the page jumps. In ASP. 1 Web NET design and development system, with one important question is how to ensure data between pages properly, safe and efficient transmission, Asp. net provides state management, and other technology to solve the problem of save and transfer data, with down. NET solve this problem under the various methods and their applicable occasions.

1. Various methods and analysis of data transfer between ASP. NET pages

1. Use Querystring method

QueryString is also called a query string, and the data to be passed is appended to the web page address (URL). If page A. aspx jumps to page B. aspx, you can use Request. Redirect(" B. aspx? Parameter name = parameter value ") method, can also use hyperlink:, page jump, in the target page can use Ruquest[" parameter name "] to receive the parameter. 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 value passed is displayed in the browser's address bar, is at risk of being tampered with, cannot pass the object, and can only be queried if the page is requested via URL.

2. Use hidden fields

Hidden fields do not show up in the user's browser. Typically, a hidden control is added to the page, and the value is assigned to the hidden control when interacting with the server and handed to the next page. A hidden domain can be any repository of web-related information stored in a web page.

Used when using a hidden field in numerical: hidden controls. value = https: / / www ofstack. com/archive 2012/04/06 / values, take: receive value variable = hidden controls. value. The advantage of using hidden fields is that they are simple to implement and are standard HTML controls that do not require complex programming logic. Hidden domains are stored and read on a page without requiring any server resources, and almost all browsers and client devices support forms with hidden domains. The disadvantage is that the storage structure is small, only support simple data structure, storage capacity is small, because it is stored in the page itself, so cannot store large values, and the large amount of data will be blocked by firewalls and agents.

3. ViewState

ViewState is a hidden form field managed by the ES52en.NET page framework. When ES54en. 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.

When passing data using ViewState: ViewState [" variable name "]= value, when fetching data: variable =ViewState[" variable name "].

The advantages of using ViewState are that the values are automatically reserved between multiple requests on the same page, no server-side resources are needed, the implementation is simple, the values in view state are hashed and compressed, and the Unicode implementation is encoded with higher security than using hidden fields. The disadvantage is that ViewState is stored on the page itself, so if you store large values, the user may display and send pages more slowly. Although view state stores data in a hash format, it can still be tampered with.

4. Use Cookie

Cookie can pass small amounts of information between pages and store it either in a text file on the client side or in memory on the client side. The Cookie method is suitable for storing information that is frequently changed in a small number of pages, such as saving login names for logged in sites, making it easy for users to enter, and saving user personalization Settings on 1 user customization items. Response. Cookies[" key name "]= key value; Fetch data with: variable name = Request.Cookies [" key name "].

The advantages of using Cookie are: Cookie is stored on the client side, no server resources are used, the implementation is simple, and the expiration time can be configured. The downside: The amount of data you can store is relatively small, and since Cookie is not supported by all browsers and may be banned or deleted by users, it cannot be used to store critical data. In addition, Cookie saves plain text in the form of simple text, in which sensitive, unencrypted text should not be stored
The data.

Use the Application variable

The Application variable is global and all users share one Application variable. Once defined, it affects all parts of the program. If you want to use a variable value, the Application object, across your application, this is the best choice. When storing data, add value to Application variable: Application[" variable name "]= value; Fetch data with: variable =Application[" Variable name "]; Explicitly clear the Application when it is not needed: Application[" quantity name "]=null.

Advantages of Application: Ease of use, global scope. All pages in the application are accessible. Disadvantages: If the server side process that holds the data is corrupted (such as due to server crash, upgrade, or shutdown), then the data will be lost, so use Application 1 to be guaranteed. Memory footprint on the server side, which can affect server performance and application scalability.

Use the Session variable

The Session object 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 individual sessions. Session USES the same method and format as Application.

Advantages: Easy to implement, provides high security and durability, can handle IIS restart and worker 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 id functionality to Web applications starting with Cookie 1, and Session can also be used in browsers that do not support Cookie. However, using Session without Cookie requires that the session identifier be placed in the query string, and you encounter the same security issues that this article stated in section 1 of the query string.

7. Use the static properties of the class

This method USES static attributes of a class to pass values between two pages. Define a class that contains static attributes; The value to be passed is assigned to the static property; The value to be passed from the source page can be obtained from the target page through static properties. The advantage is that it is convenient to transfer multiple data, but the disadvantage is that additional programming is needed to increase the workload of programming and occupy the server memory.

8. Use Server. Transfer

The Server.Transfer method is used to transfer the execution process from the current ASPX file to another ASPX page on the same server while retaining the form data or query string by setting the second parameter of the method to True and using Server.Transfer (" target page name.aspx", true) on the first page. The target page fetches data with: ES144en.Form [" Control name "] or ES146en.QueryString [" Control Name "]. Asp. es149EN2.0 can also be used as follows:

PreviousPage pg1;
pg1 = (PreviousPage) Context. Handler;
Response. Write (pg1. Name);
Previous-Page is the class name of the original page. Name is the property defined on the original page. The data to be passed is stored in this property.

Using this method, you need to write some code to create some properties so that it can be accessed on another page, and you can access the values as object properties on another page. This method is particularly useful in value passing between pages, which is both concise and object-oriented.

9. Cache

Cache has powerful data manipulation capabilities that store data as a collection of key-value pairs that can be inserted and retrieved by specifying keywords. Its dependency-based termination capabilities enable it to precisely control how and when data is updated and cached. It can be internally locked without the need for serialization management using the Lock() and Unlock() methods as with Application objects. The disadvantage is that the use method is more complex, improper use can reduce performance.

2. Value transfer method that can be adopted under different page jumps

Situation 1: The source page can jump to the target page, and the source page passes data to the target page

Using a query string, transferring a small amount of information from one page to another and not having security problems is a simple and common method. Using the ES185en.Transfer method, you can pass form data or query strings to another page, and you can also save HttpContext for the original page, which you can use when the target page and the source page are on the same server.

Case 2: The page passes values to its own page

That is, preserving the value between multiple requests on the same page, the ViewState attribute provides functionality with basic security. Hidden fields can also be used to store a small amount of page information that is sent back to itself or another page, regardless of security concerns.

Case 3: The source page passes values to the target page, and the source page cannot connect directly to the target page.

There are multiple methods, depending on the situation.

Application: Store global information that is used by multiple users and changes infrequently, so security is not an issue. Don't store a lot of information. Session: Stores short-term information specific to individual sessions and requires a high level of security. Do not store large amounts of information in 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 can take up a lot of server resources and affect scalability.

Cookie: When you need to store small amounts of information on the client and when there are no security issues. Class to facilitate the transfer 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, efficient time. The above methods are not only applicable to case 3, but can be used in both cases, but should be used sparingly when it is not necessary, otherwise it will waste resources or add complexity to the program.

Related articles: