Asp. NET page Transfer Value Introduction _ Practical Tips

Source: Internet
Author: User
Tags server memory

First, querystring

QueryString is a very simple way of passing values, and he can display the value of the transfer in the browser's address bar. You can use this method if you pass one or more values that are not of high security requirements or are simple in structure. However, this method is not available for passing arrays or objects.

The advantages of this approach are: 1. Simple to use, it is very effective to pass numbers or text values when security requirements are low.

Disadvantage of this method: 1. Lack of security because its value is exposed to the URL of the browser; 2. The object cannot be passed.

Usage: 1. Construct the URL address in the source page code with the name and value you want to pass; 2. The code on the source page is Response.Redirect (URL); redirected to the URL address above 3. The code on the destination page uses request.querystring["name"; takes out the value passed in the URL address.

Example: (1) a.aspx

1.private void Button1_Click (object sender, System.EventArgs e)
0.9
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)
0.9
3.label2.text = request.querystring["name"];
4.}
Second, session

Presumably this is the most common use of everyone, its operation is similar to application, the role of users, so, excessive storage will lead to the depletion of server memory resources.

Advantages: 1. Simple to use, not only to pass simple data types, but also to pass objects; 2. The amount of data is unlimited.

Disadvantages: 1. Storing large amounts of data in the session variable consumes more server resources; 2. Easy to lose.

Usage: 1. Create the name and value you want to pass in the code of the source page constructs the session variable: session["name"]= "value (Or Object)", 2. The code on the destination page uses the session variable to take out the passed value. result = session["Nmae"]

Note: The session can be destroyed when not, destroy the method is: Clear A: Session.remove ("Session name"); Clear all: Session.clear ();

Example: (1) a.aspx

1.private void Button1_Click (object sender, System.EventArgs e)
0.9
3.session["name"] = Label.text;
6.?
(2) b.aspx

1.private void Page_Load (object sender, EventArgs e)
0.9
3.string name;
4.name = session["Name"]. ToString ();
5.}
Third, cookies

This is also commonly used methods, cookies used in the user's browser to store small pieces of information, save the user's information, such as the user to visit a Web site when the user ID, user preferences, etc., the user next visit can be retrieved to obtain the previous information. So cookies can also pass values between pages. Cookies are passed back and forth between the browser and the server via HTTP headers. A cookie can only contain the value of a string, and if you want to store an integer value in a cookie, you need to convert to a string form first.

As with session, it is for every user, but there is an essential difference, that is, cookies are stored on the client, and the session is stored on the server side. And the use of cookies should be used in conjunction with the ASP.net built-in object request.

Advantages: 1. Simple to use, is a very common way to maintain user status. For example, in a shopping site, users can use it to maintain user status across multiple page forms.

Disadvantages: 1. It is often thought to be criticized for collecting user's privacy; 2. Security is not high, easy to forge.

Usage: 1. Create the name and value you want to pass in the code in the source page construct the cookie object:

1.HttpCookie Objcookie = new HttpCookie ("MyCookie", "hello,cookie!");
2.response.cookies.add (cookie);
2. The code on the destination page uses a cookie object to fetch the passed value: result = request.cookies["MyCookie"]. Value;

Example: (1) a.aspx

1.private void Button1_Click (object sender, System.EventArgs e)
0.9
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;
Four, application

The scope of the Application object is the entire global, which means it works for all users. It is valid throughout the application lifecycle, similar to using global variables, so it can be accessed in different pages. It differs from the session variable in that the former is a global variable shared by all users, which is a unique global variable for each user.

Some people may ask, since all users can use the application variable, then he can use in what situation? Here is an example: the number of site visits. It can be manipulated when multiple requests are accessed.

Advantages: 1. The use of simple, less consumption of server resources; 2. Not only can transfer simple data, but also transfer objects; 3. The amount of data is unlimited.

Disadvantages: 1. As a global variable, it is easy to be operated incorrectly. Therefore, the variables used by individual users are generally not application.

Usage: 1. Create the name and value you want to pass in the code of the source page construct application variable: application["Nmae"]= "value (Or Object)"; 2. The code on the destination page uses the application variable to take out the passed value. result = application["Nmae"]

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

Example: (1) a.aspx

1.private void Button1_Click (object sender, System.EventArgs e)
0.9
3.application["name"] = Label1.Text;
4.}
(2) b.aspx

1.private void Page_Load (object sender, EventArgs e)
0.9
3.string name;
4.application.lock ();
5.name = application["Name"]. ToString ();
6.application.unlock ();
7.}
Five, Server.Transfer

This can be said to be the object of the development of the method used, which uses the Server.Transfer method to guide the process from the current page to another page, the new page uses the previous page of the response stream, so this method is completely surface image object, concise and effective.

Server.Transfer is to go from the current ASPX page to the new ASPX page, the server side executes the new page and output, in the new page through the Context.Handler to obtain the data types of the previous page passed the values, form data, QueryString. Because redirection is complete on the server side, the URL address in the client browser will not change. When Server.Transfer is invoked, the current ASPX page terminates execution, and the execution process is transferred to another ASPX page, but the new ASPX page still uses the reply stream created by the previous ASPX page.

PS: Compare the difference between Server.Transfer and Response.Redirect.

(1) Server.Transfer is done on the server side, so the URL address in the client browser is not changed, Response.Redirect is the client completes, and a new page processing request is made to the server side, so the URL address in the client browser will change.

(2) Server.Transfer is done on the server side without requiring a client to make a request, reducing the client's request to the server.

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

(4) Server.Transfer can upload various types of values from the previous page to the new page, Response.Redirect can upload various types of values to the new page with the help of a URL or a combination of the above four methods.

Advantages: 1. Direct in the server side orientation, easy to use, reduce the client to the server side of the request; 2. You can pass values of various data types and the values of controls.

Disadvantages: 1. The URL address in the client browser does not change, causing some unexpected problems that may occur in the new page. For example, if the source page and destination page are not in the same virtual directory or its subdirectories, then pictures and hyperlinks using relative paths can cause errors to be pointed to.

Usage: 1. In the source page code, use the page class's server.transfer to jump to another page to pass the paging data: Server.Transfer ("B.aspx", "false") 2. In the destination page, use Context.Handler to receive data: formerpage formerpage = (formerpage) Context.Handler; Then use Formerpage properties and methods to get the value of the previous page, or use context.items["Myparameter" directly.

Example: (1) a.aspx

1.public string Name
0.9
3.get{return Label1.Text;}
6.?
5.private void Button1_Click (object sender, System.EventArgs e)
4.9
7.server.transfer ("b.aspx");
8.}
(2) b.aspx

1.private void Page_Load (object sender, EventArgs e)
0.9
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.