Several methods for transferring data between ASP. NET pages

Source: Internet
Author: User

This article will discuss several methods for transferring data between ASP. NET pages, hoping to help you understand the usefulness and convenience of transferring data between ASP. NET pages.

0. Introduction

The Web page is stateless, and the server considers each request to come from different users. Therefore, the status of the variable is not retained between consecutive requests to the same page or when the page jumps. ASP. when designing and developing a Web system, you may encounter an important problem: how to ensure that data is transmitted correctly, securely, and efficiently between pages, asp.net provides multiple technologies, such as status management, to save and transmit data.. NET.

  • ASP. NET Page object model: Page class Introduction
  • Key events and execution steps of ASP. NET page Lifecycle
  • Several methods for transferring values between ASP. NET pages
  • Several experiences of ASP. NET page Cache
  • Details about the aspx extension of ASP. NET pages

1. Methods and analysis of data transmission between ASP. NET pages

1.1 Use the Querystring Method

QueryString is also called a query string. The data to be transmitted in this method is appended to the webpage address (URL) for transmission. For example, when page A. aspx jumps to page B. aspx, you can use Request. Redirect ("B. aspx? Parameter Name = parameter value ") method, you can also use a hyperlink:, after the page jumps, you can use Ruquest [" parameter name "] on the target page to receive parameters. The QuerySting method is easy to implement and does not use server resources. The disadvantage is that the passed value is displayed on the address bar of the browser, which may cause tampering and cannot pass objects, it is feasible to query strings only when the URL request page is passed.

1.2 use hidden Domains

Hidden fields are not displayed in the user's browser. Generally, a hidden control is added to the page. when interacting with the server, the value is assigned to the hidden control and submitted to the next page. A hidden domain can be a repository of any webpage-related information stored on a webpage. When using a hidden field to store values, use the: hidden control. value = value. When getting the received value, use the: Variable = hidden control. value. The advantage of hiding a domain is that it is easy to implement. Hiding a domain is a standard HTML control without complicated programming logic. Hidden domains are stored and read on pages without any server resources. Almost all browsers and client devices support forms with hidden domains. The disadvantage is that the storage structure is small, and only a simple data structure is supported. The storage capacity is small because it is stored on the page itself, so it cannot store large values, in addition, a large amount of data is 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 value and all controls on the page are collected and formatted into an encoding string, and then assigned to the value attribute of the hidden form field. When you use ViewState to pass data, you can use ViewState ["variable name"] = value. When you retrieve data, use ViewState ["variable name"]. ViewState has the following advantages: the value is automatically retained among multiple requests on the same page, without the need for server resources. This is simple, and the values in the view State are hashed and compressed, in addition, for Unicode implementation encoding, its security is higher than the use of hidden fields; disadvantage is that ViewState is stored on the page itself, if
The storage of large values may slow down when users display pages and send pages. Although the view State stores data in a hash format, it can still be tampered.

1.4 use cookies

Cookies can transmit a small amount of information between pages. They can be stored in client text files or in client memory. The Cookie method is suitable for storing a small amount of information that is frequently modified on the page. For example, it is convenient to save the login user name for a logged-in website, you can also save your personalized settings on custom projects. When data is transmitted using Cookies, the following functions can be used: Response. Cookies ["key name"] = key value; data retrieved using: variable name = Request. Cookies ["key name"]. The advantage of using cookies is that the Cookie is stored on the client and does not use server resources. It is easy to implement and can be configured with the expiration time. The disadvantage is that the data volume that can be stored is small. Because cookies are not supported by all browsers and may be disabled or deleted by users, they cannot be used to store key data. In addition, cookies are stored in plain text, which cannot store sensitive and unencrypted text.
Data.

1.5 Use the Application variable

You can use the Application variable to transfer values between pages. The Application variable is global and all users share an Application variable. Once defined, it will affect all parts of the program. It is the best choice to use a variable value Application object throughout the Application. When saving the data, add the value to the Application variable: Application ["variable name"] = value; Use the variable = Application ["variable name"] to retrieve the data. when you do not need to use this Application, you need to explicitly clear it: Application ["volume name"] = null.
Application Advantages: easy to use and global scope. All pages in the program are available for access. Disadvantage: if the server-side Process storing data is damaged, such as because the server crashes, upgrades, or shuts down, the data will be lost, so the Application must have a minimum protection policy; this may affect the server performance and scalability of applications.

1.6 use Session Variables

The Session object can be used to store the information of the specified conversation to be maintained. Different clients generate different Session objects. Sessions are used to store short-term information specific to individual sessions. The usage and format of the Session are the same as those of the Application.

Advantages: it is easy to implement and provides high security and durability. It can be used in multi-process scenarios to cope with IIS restart and secondary process restart. The disadvantage is that the server memory is consumed. Therefore, do not store a large amount of information. The most common purpose of Session is to provide the user identification function to Web applications together with cookies. sessions can also be used in browsers that do not support cookies. However, when using a Session without a Cookie, you need to place the Session identifier in the query string, which also meets the security issues described in the query string section.

1.7 use static attributes of a class

This method uses static attributes of the class to implement value transfer between two pages. Defines a class containing static attributes. values to be transferred are assigned to static attributes. values to be transmitted on the source page can be obtained through static attributes on the target page.
The advantage is that it can easily transmit multiple data. The disadvantage is that additional programming is required to increase the workload of programming and occupy the server memory.

1.8 use Server. Transfer

Through Server. the Transfer method transfers the execution flow from the current ASPX file to another ASPX page on the same server, while retaining form data or querying strings, set the second parameter of this method to True, and use Server on the first page. transfer ("target Page name. aspx ", true); used to retrieve data from the target page: Ruquest. form ["Control name"] Or Ruquest. queryString ["Control name"]. Asp. net2.0 can also be used as follows:

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

Note: This code is used to retrieve the passed value from the target Page. Previous-Page is the class Name of the original Page, and Name is the attribute defined on the original Page, the data to be transferred is stored in this attribute.
To use this method, you need to write some code to create some attributes so that you can access it on another page. You can access the value on another page in the form of object attributes, this method is particularly useful in inter-page value transfer. This method is not only concise, but also object-oriented.

1.9 Cache

Cache has powerful data operations. It stores data in the form of a set of key-value pairs. You can specify keywords to insert and retrieve data items. Its Dependency-based termination function enables it to precisely control how to update and eliminate slow data in a timely manner. It can implement internal Lock Management and does not need to use the Lock () and Unlock () methods as the Application object for serial management. The disadvantage is that the use method is complicated, and improper use reduces the performance.

2. The value passing method can be used for different page jumps

2.1 Case 1: The Source Page jumps to the target page, and The Source Page passes data to the target page.

It is a simple and common method to transmit a small amount of information from one page to another using a query string and avoid security issues. the Transfer method can pass form data or query strings to another page, and 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.2 Case 2: pass a value to the page

That is, the value is retained between multiple requests on the same page. The ViewState attribute provides basic security functions. It can also be used to store a small amount of page information sent back to itself or another page, without considering security issues.

2.3 Case 3: The Source Page passes the value to the target page, and The Source Page cannot be directly connected to the target page.

There are multiple methods. The specific use depends on the specific situation.

Application: stores global information that is used by multiple users and is not frequently changed. Security is not a problem at this time. Do not store a large amount of information. Session: stores short-term information specific to individual sessions, and requires high security. Do not store a large amount of information in the 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 may occupy a large amount of server resources and affect scalability.

Cookie: It is used when you need to store a small amount of information on the client and there is no security problem. Class to facilitate the transmission of multiple data.

Cache: objects are used by a single user, a group of users, or all users. Data can be stored for multiple requests for a long time and efficiently. The above methods are not only used in case 3, but can be used in both cases, but are used as little as possible if there is no need, otherwise it will cause a waste of resources or increase the complexity of the program.

Original article title:Data transfer between ASP. NET pages

Link:Http://www.cnblogs.com/gsanidt/archive/2009/12/15/1624553.html

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.