Several methods for transferring data between ASP. NET pages

Source: Internet
Author: User

 

What we will discuss in this article is:Asp.netSeveral methods of data transmission between pages, hoping to help you understand the usefulness and convenience of data transmission between ASP. NET pages correctly.

1. Introduction

Web pages are stateless,
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.

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

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, only when the URL
It is feasible to query strings when requesting pages.

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. Use the: Den den control to store hidden fields in numeric values. Value = value. When receiving the 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.

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, which is simple. The value in view State is hashed and compressed, and is applicable to Unicode
The security of viewstate is higher than that of hidden fields. The disadvantage is that viewstate is stored on the page.

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.

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"]. Use cookie
The advantage is that the cookie is stored on the client and does not use server resources. It is easy to implement and the expiration time can be configured. 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. Therefore, they are not suitable for storing sensitive and unencrypted data.

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. you do not need to use this application
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 that stores data is damaged (for example, 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.

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.