Summary of the ASP.NET interpage data transfer method

  • 2020-05-07 19:29:06
  • OfStack

0, introduction

Web pages are stateless, and the server considers each request to be from a different user, so the state of the variable is not preserved between successive requests to the same page or during 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 of data transmission and analysis

1.1 use the Querystring method
QueryString is also called a query string, and the data to be passed in this method is passed after the web address (URL). If A.aspx goes to B.aspx, Request.Redirect (" B.aspx? Parameter name = parameter value ") method, can also be hyperlinked: < a href = "B aspx? Parameter name = parameter value" > After the page has jumped, Ruquest[" parameter name "] can be used in the target page to receive parameters. The advantage of using the QuerySting method is that the implementation is simple and no server resources are used. The disadvantage is that the passed values are displayed in the browser's address bar, there is a risk of tampering, no object can be passed, and querying the string is only feasible if the page is requested through URL.


1.2 makes use of the hidden field
Hidden fields are not displayed in the user's browser, 1 is generally to add a hidden control in the page, when interacting with the server to assign the value to the hidden control and submit to the next 1 page. A hidden field can be any repository of web-related information stored in a web page. hidden control.value= value when storing value with hidden field, hidden control.value control when fetching and receiving value. The advantage of using hidden fields is that they are simple to implement. Hidden fields are standard HTML controls that do not require complex programming logic. Hidden fields are stored and read on the page, no server resources are required, and almost all browsers and client devices support forms with hidden fields. The disadvantage is that the storage structure is small, only simple data structure is supported, storage is small, because it is stored in the page itself, so it cannot store large values, and the large amount 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 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 with ViewState: ViewState [" variable name "]= value, when fetching data: variable =ViewState[" variable name "]. The advantage of using ViewState is that it can automatically reserve the value between multiple requests on the same page without using server-side resources, and it is simple to implement. The value in view state is hashed and compressed, and it is encoded for Unicode implementation, which is more secure than using hidden field. The disadvantage is that ViewState is stored in the page itself, so if
Storing large values can slow down the user's ability to display and send pages. Although view state stores data in hash format, it can still be tampered with.
*

1.4 USES Cookie
Cookie can pass small amounts of information between pages and can be stored in a text file on the client side or in memory on the client side. The Cookie method is suitable for storing a small number of frequently changed information on the page, such as saving the login user name for the logged site, providing convenience for user input, and saving the user's personalized Settings on some user-defined items. Cookie: 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, simple implementation, configurable expiration time. The downside: the amount of data that can be stored is small, and because Cookie is not supported by all browsers and can be disabled or deleted by users, it cannot be used to save critical data. In addition, Cookie is stored in plain text, which is not suitable for storing sensitive, unencrypted text
The data.
*

1.5 USES the Application variable
also USES Application variable to achieve the transfer between pages, Application variable is global, all users share 1 Application variable, 1 definition, it will affect all parts of the program. The Application object is the best choice if you want to use a variable value throughout the application. When storing data, add the value to the Application variable: Application[" variable name "]= value; Take out the data with: variable =Application[" variable name "]; When the Application is not needed, clear it explicitly: Application[" quantity name "]=null.
Application advantages: easy to use, global scope. Accessible to all pages in the application. Disadvantages: if the server-side process where the data is stored is corrupted (such as by a server crash, upgrade, or shutdown), then the data is lost, so there must be a guarantee policy for Application 1; Server side memory, which can affect server performance and application scalability.
*

1.6 USES the Session variable
The
Session object can be used to store information about specific conversations that need to be maintained, and different clients generate different Session objects. Session is used to store short-term information specific to a single session. Session is used in the same way and format as Application.
Advantages: easy to implement and provides high security and persistence against IIS restarts and secondary process restarts, which can be used in multiple processes. The disadvantage is that it consumes memory on the server side. So don't store a lot of information. The most common use of Session is to provide userid functionality to Web applications, starting with Cookie 1. Session is also available for browsers that do not support Cookie. However, using Session without Cookie requires placing the session identifier in the query string, which again presents the same security issues described in section 1 of this article.
*

1.7 USES the static properties of the class
is a method that USES the static properties of a class to pass values between two pages. Define a class that contains static properties; The value to be passed is assigned to a 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 can be convenient to transfer multiple data, the disadvantage is that the need for additional programming, increase
The workload of program design, occupying server memory.
*

1.8 USES Server.Transfer
The Server.Transfer method can be 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. The target page retrieves the data with Ruquest.Form [" control name "] or Ruquest.QueryString [" control name "]. Asp.net2.0 can also be used as follows:  

PreviousPage pg1;  
pg1 = (PreviousPage) Context. Handler;  
Response. Write (pg1. Name);

Description: this code is used in the target page to fetch the passed value, Previous-Page is the class name of the original page, Name is the property defined in the original page, the data to be passed into this property.
Using this method, you need to write some code to create some properties that can be accessed on another page, and you can access values as object properties on another page. This method is particularly useful in passing values between pages, and it is both concise and object-oriented.

1.9 Cache
Cache has powerful data manipulation capabilities, stores data as a collection of key-value pairs, and can insert and retrieve data items by specifying keywords. Its dependability based termination enables it to precisely control how and in a timely manner the data in the cache is updated and eliminated. It can be managed internally and does not require the serialization of Lock() and Unlock() methods as Application objects. The disadvantage is that the use method is more complex, use improper but reduce the performance.



2, different page jump can be used in the case of the value method

*
2.1 case 1: the source page can jump to the target page, and the source page passes data to the target page
Using a query string to transfer a small amount of information from one page to another with no security issues is a simple and common method. Using the Server.Transfer method, you can pass form data or query strings to another page, and you can also save HttpContext of the original page, which you can use when the target page and the source page are on the same server.
*

2.2 case 2: a page passes a value to its own page

That is, the ViewState attribute provides basic security by reserving values between multiple requests on the same page. 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.



Case 3: the source page passes a value to the target page, and the source page cannot connect directly to the target page.
There are more than one method, the specific use of which depends on the specific situation.
Application: stores global information that is used by multiple users and changes infrequently, where security is not an issue. Don't store large amounts of information. Session: stores short-term information specific to a single session and requires high security. Do not 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 an application that supports many users, this can take up a lot of server resources and affect scalability.
Cookie: used when you need to store a small amount of information on the client and there are no security issues. Class to facilitate the transfer of multiple data.
Cache: object for a single user, 1 group of users, or all users. You can save data for multiple requests for a long time and efficiently. The above methods are not only used in case 3. You can use them in both cases, but use them sparingly when they are not necessary, which can lead to a waste of resources or increase the complexity of the program.
Reference: http: / / gsanidt. cnblogs. com


Related articles: