ASP. NET -- transfer data between pages using hidden Domains

Source: Internet
Author: User
Tags server memory

 

Abstract:

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.

Usage:

Use the: Den den control to store hidden fields in numeric values. Value = value. When receiving the value, use the variable = hidden control. Value.

Advantages:

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.

Disadvantages:

The storage structure is small. It only supports a simple data structure with less storage capacity. Because it is stored on the page itself, it cannot store large values, in addition, a large amount of data is blocked by firewalls and proxies.

Recently, I used a hidden domain to transfer the value of my car rental!

. Net also has many methods to pass values: The following are from csdn

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

 

<Divstyle = "position: relative; width: 80px; Height: 30px; margin: 20px auto; text-align: center;">
<Inputid = "lyfwpath" type = "hidden" value = ""/>
<Inputid = "lyfwsubmit" type = "button" value = "" class = "button4"/>
<Spanvalidator = "validator" vsubmit = "lyfwsubmit" vsubmitpath = "common/clientorderformoperator. aspx? Type = lyfw "vsubmitfunction =" "vsubmitcallbackfunction =" lyfwsubmitcallback ">
<Spanvtarget = "lvfwtime" vtargetvalue = "value" vtype = "required"> </span>
<Spanvtarget = "lvfwtime2" vtargetvalue = "value" vtype = "required"> </span>
<Spanvtarget = "lyfwpath" vtargetvalue = "value" vtype = "required"> </span>
<Scripttype = "text/JavaScript">
$ ("* [Name = 'lvyouluxian ']"). Click (function (){
$ ("# Lyfwpath"). ATTR ("value", $ (this). ATTR ("ID "));
})
Function lyfwsubmitcallback (result)
{
If (result = "succeed ")
{
Window. Location = "selectbus. aspx ";
}
Else if (result = "datetimeerror ")
{
Alert ("select the departure time correctly! ");
}
Else
{
Alert ("failed to submit ");
}
}
</SCRIPT>
</Span>
</Div>

 

 

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.

 

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, it is feasible to query strings only when the URL request page is passed.

 

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"]. 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. Disadvantage: the amount of data that can be stored is relatively small, because the cookie
It is not supported by all browsers and may be disabled or deleted by users. Therefore, it cannot be used to save 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. 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 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.

 

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.

 

7. Use static attributes of the 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.

 

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:

 

Previouspage PG1;

 

PG1 = (previouspage) Context. Handler;

 

Response. 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.

 

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.

 

Value transfer method applicable to different page jumps

 

1. Scenario 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. 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.

 

3. Case 3: the value of the Source Page is passed 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.

Guangji grain, build a high wall, slowly claim the king
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.