Asp. NET page data transmission methods and analysis

Source: Internet
Author: User
Tags numeric numeric value require requires sessions server memory advantage

The Web page is stateless and the server is considered to be from different users for each request, so the state of the variable is not retained between successive requests for the same page or when the page jumps. When designing and developing a web system with asp.net, an important problem is how to ensure that the data is transmitted correctly, safely and efficiently between pages, ASP.net provides a variety of technologies such as state management to solve the problem of saving and passing data to discuss. NET  To solve this problem in various ways and their respective applications.





1.1 using QueryString method


QueryString is also called a query string, which is passed after the page address (URL) is appended to the data. such as page a.aspx jump to the page b.aspx, you can use Request.redirect ("b.aspx? parameter name = parameter value") method, you can also use hyperlinks:, page jump, in the target page to be available ruquest["parameter name" to receive parameters. 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 on the address bar of the browser, there is a risk of tampering, cannot pass the object, only the query string is feasible when the page is requested through a URL.





1.2 using hidden domain





hidden fields are not displayed in the user's browser, typically by adding a hidden control to the page, assigning values to the hidden controls when interacting with the server and submitting them to the next page. A hidden field can be any repository of Web page-related information stored in a Web page. Use hidden fields to save values by: Hidden control. value= numeric value, take out receive value: Variable =hidden control. Value. The advantage of using hidden fields is that it is simple to implement, and hidden fields are standard HTML controls that do not require complex programming logic. Hidden fields are stored and read on the page and do not require any server resources, and almost all browsers and client devices support forms with hidden fields. The disadvantage is that there is little storage structure, only supporting a simple data structure, less storage, because it is stored in the page itself, so can not store large values, and large amount of data will be blocked by firewalls and agents.





1.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 a coded string, which is then assigned to the Value property of the hidden form field. Using ViewState to pass data is available: ViewState [variable name]= numeric value, with: variable =viewstate[variable name when fetching data]. The advantage of using ViewState is that the value is automatically preserved between multiple requests on the same page without server-side resources, the values in view state are hashed and compressed, and encoding for Unicode implementations is more secure than using hidden fields; The disadvantage is that viewstate stored in the page itself, so if the


stores large values that can slow down when users display pages and send pages. Although view state stores data in hashing format, it can still be tampered with.





1.4 Using Cookies




The
Cookie can pass a small amount of information between pages, can be stored in the client's text file, or stored in the client's memory. The Cookie method is useful for storing information that is often changed in a small number of pages, such as saving a login username for a logged on Web site, facilitating user input, and saving user personalization on some user-defined items. The use of cookies to pass data is available by: response.cookies["key Name"]= key value, and fetching 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, are simple to implement, and can configure expiration times. The disadvantage is that the amount of data that can be stored is small, and because cookies are not supported by all browsers and may be blocked or deleted by users, they cannot be used to save critical data. In addition, the Cookie is stored in the form of simple plaintext text, which is not suitable for storing sensitive, unencrypted


data.





1.5 uses Application variable





Use Application variables can also achieve the 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 application object throughout your application scope, you will be the best choice. When you save the data, add the value to the application variable: application["variable name"]= numeric value, remove the data by: variable =application["variable name", and when you do not need to use the application,  To explicitly clear it: application[the]=null of the quantity name.


Application Advantages: Easy to use, global scope. Accessible to all pages in the application. Disadvantage: If the server-side process that holds the data is corrupted (such as damage due to server crashes, upgrades, or shutdowns), the data will be lost, so use application must have a guaranteed policy; Consuming server-side memory, which can affect server performance and application scalability.





1.6 Using the session variable




The
session object can be used to store information about a specified conversation that needs to be maintained, and different clients generate different sessions objects. Sessions are used to store short-term information that is specific to a single session.  The session uses the same method and format as application.





Benefits: Easy to implement, and provides high security and durability that can be used in multiple processes for IIS restart and worker process restarts. The disadvantage is that the server-side memory is consumed. So don't store a lot of information. The most common use of session is to provide a user identification feature to a Web application with cookies, and a session can be used for browsers that do not support cookies. However, using a session without a cookie requires you to 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 uses static properties of a class





This approach is to use the static properties of a class to achieve value transfer between two pages.  Defines a class that contains static properties, assigns a static property to a value to be transferred, and obtains values from the source page through static properties in the destination page.

The advantage of
is that it is convenient to transfer multiple data, the disadvantage is that additional programming is needed to increase the workload of program design and to occupy server memory.





1.8 uses Server.Transfer





can preserve the form data or query string by using the Server.Transfer method to move the execution process from the current ASPX file to another ASPX page on the same server, by setting the second argument of the method to true. On the first page with Server.Transfer ("Destination page name. aspx", true), the target page takes out data using: 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 take out the value passed in the target page, Previous-page is the class name of the original page, name is the attribute defined in the original page, and the data that needs to be passed into this attribute.


Use this method, you need to write some code to create some properties so that you can access it on another page, you can access the values in the form of object properties on another page, this method is particularly useful in the transfer of values between pages, this method is not only concise, but also object-oriented.





1.9 Cache





Cache has a powerful data manipulation function to store data in the form of a key-value pair, and you can insert and retrieve data items by specifying a keyword. Its dependency-based termination capabilities enable it to accurately control how and when data in the cache is updated and eliminated in a timely manner. It can be locked internally and does not require serialization management using the lock () and Unlock () methods like the Application object.  The disadvantage is that the use of the method is more complex, the use of improper rather reduce performance.





2, different page jumps can be used in the value-transfer method





2.1 Situation One: The source page can jump to the target page, the source page passes the data to the target page





is a simple and common method of using query strings, transferring small amounts of information from one page to another, and not having security issues; by using the Server.Transfer method, you can pass form data or query strings to another page, and you can save the initial page's HttpContext. This method can be used when the target page and the source page are on the same server.





2.2 Situation Two: The page passes the value to own page





That is, a value is preserved between multiple requests on the same page, and the ViewState property provides functionality with basic security. You can also use hidden fields to store a small number of pages that are sent back to itself or another page, and are used without regard to security issues.





2.3 Situation Three: The source page passes the value to the target page, and the source page cannot be directly connected to the target page.





There are several methods, specifically with which to see the specific situation.





Application: Stores global information that is used by multiple users and that 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 individual sessions and requires higher 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 applications that support many users, this can consume a large amount of server resources and affect scalability.





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





Cache: An object is used for a single user, a group of users, or all users. You can save data for a long, efficient time for multiple requests. These methods are not only used in case three, the first two cases can be used, but do not need to use as little as possible, otherwise it will cause a waste of resources or increase the complexity of the program.

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.