Introduction to how to pass values on ASP. NET pages

Source: Internet
Author: User

I. QueryString

QueryString is a simple method for transferring values. It can display the transmitted values in the address bar of a browser. This method can be used to transmit one or more numeric values with low security requirements or simple structure. However, this method cannot be used to pass arrays or objects.

Advantages of this method: 1. Simple to use, it is very effective to transmit numbers or text values when security requirements are not high.

Disadvantages of this method: 1. Lack of security, because its value is exposed in the URL address of the browser; 2. The object cannot be passed.

Usage: 1. construct a URL address using the name and value to be passed in the Code on the Source Page; 2. the Code on the Source Page uses Response. redirect (URL); Redirect to the above URL; 3. the code on the target page uses Request. queryString ["name"]; retrieves the value passed in the URL.

Example: (1) a. aspx

1. private void button#click (object sender, System. EventArgs e)
2 .{
3. string s_url;
4. s_url = "B. aspx? Name = "+ Label1.Text;
5. Response. Redirect (s_url );
6 .}
(2) B. aspx

1. private void Page_Load (object sender, EventArgs e)
2 .{
3. Label2.Text = Request. QueryString ["name"];
4 .}
Ii. Session

Presumably, this is definitely the most common usage. Its operations are similar to those of the Application, which act on individual users. Therefore, excessive storage will result in the depletion of server memory resources.

Advantages: 1. Simple to use, not only can pass simple data types, but also can pass objects; 2. There is no limit on the data size.

Disadvantages: 1. storing a large amount of data in the Session variable will consume a lot of server resources; 2. easy to lose.

Usage: 1. create the Name and Value you need to pass in the Code on the Source Page to construct the Session variable: Session ["Name"] = "Value (Or Object)"; 2. the code on the target page uses the Session variable to retrieve the passed value. Result = Session ["Nmae"]

Note: A session can be destroyed when it is not used. The destroy method is: Clear Session. Remove ("session name"); Clear all: Session. Clear ();

Example: (1) a. aspx

1. private void button#click (object sender, System. EventArgs e)
2 .{
3. Session ["name"] = Label. Text;
4 .}
(2) B. aspx

1. private void Page_Load (object sender, EventArgs e)
2 .{
3. string name;
4. name = Session ["name"]. ToString ();
5 .}
3. Cookie

This is also a common method. Cookies are used to store small pieces of information in a user's browser and store user-related information, such as the user ID and user preferences when a user accesses a website, the user can retrieve the previous information from the next visit. Therefore, cookies can also pass values between pages. The Cookie is sent back and forth between the browser and the server through the HTTP header. A Cookie can only contain string values. If you want to store an integer value in a Cookie, you must first convert it to a string.

Like Session, Cookie is stored on the client and session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request.

Advantages: 1. Easy to use, is a very common method to maintain the user status. For example, you can use a shopping website to maintain the user status when a user spans multiple page forms.

Disadvantages: 1. People often think that it is used to collect user privacy and are criticized; 2. Low Security and easy to forge.

Usage: 1. Create the name and value you need to pass in the Code on the Source Page to construct the Cookie object:

1. HttpCookie objCookie = new HttpCookie ("myCookie", "Hello, Cookie! ");
2. Response. Cookies. Add (cookie );
2. The code on the target page uses the Cookie object to retrieve the passed Value: Result = Request. Cookies ["myCookie"]. Value;

Example: (1) a. aspx

1. private void button#click (object sender, System. EventArgs e)
2 .{
3. HttpCookie objCookie = new HttpCookie ("myCookie", "Hello, Cookie! ");
4. Response. Cookies. Add (objCookie );
5 .}
(2) B. aspx

1. string myName1Value;
2. myName1Value = Request. Cookies ["myCookie"]. Value;
Iv. Application

The scope of the Application object is global, that is, it is valid for all users. It is effective throughout the application lifecycle, similar to using global variables, so it can be accessed on different pages. The difference between it and Session variables is that the former is a global variable shared by all users, and the latter is a global variable unique to each user.

Some may ask, since all users can use the application variable, where can they be used? Here is an example of the number of website regions. You can perform operations on multiple requests.

Advantages: 1. Simple to use and consume less server resources; 2. Not only simple data can be passed, but also objects can be passed; 3. Unlimited data size.

Disadvantage: 1. As a global variable, it is prone to misoperations. Therefore, application is generally unavailable for variables used by a single user.

Usage: 1. create the name and Value you need to pass in the Code on the Source Page to construct the Application variable: Application ["Nmae"] = "Value (Or Object)"; 2. the code on the target page uses the Application variable to retrieve the passed value. Result = Application ["Nmae"]

Note: The lock and unlock methods are often used to lock and unlock, in order to prevent concurrent modification.

Example: (1) a. aspx

1. private void button#click (object sender, System. EventArgs e)
2 .{
3. Application ["name"] = Label1.Text;
4 .}
(2) B. aspx

1. private void Page_Load (object sender, EventArgs e)
2 .{
3. string name;
4. Application. Lock ();
5. name = Application ["name"]. ToString ();
6. Application. UnLock ();
7 .}
V. Server. Transfer

This can be said to be the method used for face object development. It uses Server. the Transfer method directs the process from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is completely object-like and concise and effective.

Server. transfer Transfers the current ASPX page to the new ASPX page. The server executes the new page and outputs the new page through Context. handler to obtain the values, form data, and QueryString of various data types passed on the previous page. because the redirection is completed on the server side, the URL in the client browser will not change. When Server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to another ASPX page. However, the new ASPX page still uses the response stream created on the previous ASPX page.

Ps: Compare the differences between Server. Transfer and Response. Redirect.

(1) Server. transfer is completed on the server, so the URL in the client browser will not change; Response. redirect is completed by the client and a new page Processing request is submitted to the server, so the URL address in the client browser will change.

(2) The Server. Transfer is completed on the Server without the need for a request from the client. This reduces the number of requests sent from the client to the Server.

(3) Server. Transfer can only jump to the page specified by the local virtual directory, that is, the page in the project, while Response. Redirect is very flexible and can jump to any URL address.

(4) Server. transfer can upload values of the previous page to the new page; Response. redirect can only use parameters in the URL or the above four methods to upload various types of values to the new page.

Advantages: 1. Direct redirection on the server side, easy to use, reduces the number of requests sent from the client to the server; 2. Various data types and control values can be transferred.

Disadvantages: 1. the URL address in the client browser is unchanged, which may cause unexpected problems on the new page. For example, if the source page and the target page are not in the same virtual directory or their sub-directories, images and hyperlinks using relative paths will lead to incorrect pointing.

Usage: 1. in the code of the Source Page, use the Server of the Page class. transfer jumps to another page to pass the page data: Server. transfer ("B. aspx "," false "); 2. on the target page, use Context. handler to receive data: FormerPage formerPage = (FormerPage) Context. handler; then, use the attributes and methods of formerPage to obtain the value of the previous page, or directly use Context. items ["myParameter"]

Example: (1) a. aspx

1. public string Name
2 .{
3. get {return Label1.Text ;}
4 .}
5. private void button#click (object sender, System. EventArgs e)
6 .{
7. Server. Transfer ("B. aspx ");
8 .}
(2) B. aspx

1. private void Page_Load (object sender, EventArgs e)
2 .{
3. a newWeb; // instance a form
4. newWeb = (source) Context. Handler;
5. string name;
6. name = newWeb. Name;
7 .}

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.