Asp. Net page pass-through value

Source: Internet
Author: User
Tags server memory

00. 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 the data is delivered correctly, securely and efficiently across the page, and that ASP provides a variety of technologies such as state management to solve the problem of saving and transmitting data to discuss.  The various methods of solving this problem and the respective applicable occasions. 1, ASP. NET page data transfer between various methods and analysis 1.1 using the QueryString method QueryString is also called a query string, this method will pass the data appended to the Web page address (URL) to pass. 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. 1.2 Hiding a field with hidden fields does not appear 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: The hidden control. Value= value, which is used when the value 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. 1.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 a large value is stored, the user may slow down when displaying the page and sending the page.  Although view state stores data in a hashed format style, it can still be tampered with. 1.4 Using 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. 1.5 using the application variable to use the application variable can also 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.
1.6 Using Session Variables
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. 1.7 Using static properties of a class This method uses static properties of the class to pass the values 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. When a user accesses, OK.  A large number of users simultaneously access, finished. 1.8 Use Server.Transfer to persist the form data or query string while the execution process is transferred from the current ASPX file to another ASPX page on the same server by using the Server.Transfer method, by setting the second parameter of the method to True , on the first page with 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;
2 pg1= (previouspage) Context.Handler;
3 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,The value can be accessed in another page as an object property, this method is particularly useful in the transfer of values between pages, whichnot only concise, but also object-oriented。 The 1.9 cache cache has powerful data manipulation capabilities that store data in the form of key-value pairs, and 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.2, different page jump case can be used in the method of transmission value2.1 Situation One:source page can jump to the target page, the source page to pass the data to the target page1, using query string, transfer a small amount of information from one page to another and no security issues, is a simple and common method, 2, using the Server.Transfer method, you can pass the form data or query string to another page, you can also save the initial page of the HttpContext,  This method can be used when the target page and the source page are on the same server. 2.2 Case Two: the page passes the value toSelf pageThat is, on thekeep values between multiple requests on the same page,ViewStateProperty provides functionality that has basic security. Also availableHidden Fields, which is used when storing small amounts of page information that is sent back to itself or another page, regardless of security issues.  2.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 methods, not only for the situation of three, the first two kinds of situations can be used, but not necessary when possible to use less,Otherwise, it can waste resources or increase the complexity of the program.。

Asp. Net page pass-through value

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.