Asp. NET pages, and several ways to pass values between

Source: Internet
Author: User
Tags server memory

Page pass-through is a problem that will be faced in the early days of the study, such as page pass value, store object value, Ajax, class, model, form, etc. But generally speaking, it is simpler to use Querystring,session,cookies,application,server.transfer.

  First, QueryString

QueryString is a very simple method of transmitting values that can be displayed in the browser's address bar. You can use this method if you are passing one or more security requirements that are not high or have a simple structure. However, you cannot use this method for passing arrays or objects.

  advantages of this approach : 1. Simple to use, it is very effective to pass a number or text value when the security requirements are not high.
  disadvantages of this approach : 1. Lack of security, because its value is exposed in the browser's URL address.
2. The object cannot be passed.

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

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {string s_url;   S_url = "B.aspx?name=" + Label1.Text; Response.Redirect (S_url); }

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {label2.text = request.querystring["name"];}

  Second, Session

  Presumably this is the most common usage of everyone, its operation is similar to application, acting on the user's personal, so, excessive storage will lead to the depletion of server memory resources.

  Pros : 1. Simple to use, not only to pass simple data types, but also to pass objects.
2. The size of the data volume is unlimited.

  cons : 1. Storing large amounts of data in session variables consumes more server resources.

2. Easy to lose.

How to use: 1. Create the name and value you need to pass in the code of the source page construct the Session variable: session["name"]= "Value (Or Object)";

2. The code in the destination page uses the session variable to remove the passed value. Result = session["Nmae"]

  Note : The session can be destroyed when not used, the method is: Clear one: Session.remove ("Session name");

Clear all: Session.clear ();

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {session["name"] = Label.text;}

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {string name; Name = session["Name"]. ToString (); }

Third, cookies

  This is also a common use of the method, the cookie is used to store small pieces of information in the user's browser, save the user's relevant information, such as user access to a website user ID, user preferences, etc., the next time the user can retrieve the previous information. So cookies can also pass values between pages. Cookies are passed back and forth between the browser and the server via an HTTP header. A cookie can contain only the value of a string, and if you want to store an integer value in a cookie, you need to first convert to a string.

As with the session, it is for each user, but there is an essential difference, that is, the cookie is 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.

  Pros : 1. Simple to use, is a very common way to maintain user state. For example, in a shopping site, users can use it to maintain user status when they cross multiple page forms.

  cons : 1. It is often criticized for collecting user privacy.

2. Security is not high, easy to forge.

  

How to use: 1. Create a Cookie object in the source page code that creates the name and value you want to pass:

2. The code on the destination page uses the cookie object to remove the value passed: Result = request.cookies["MyCookie"]. Value;

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {HttpCookie Objcookie = new HttpCookie ("MyCookie", "hello,c  Ookie! "); RESPONSE.COOKIES.ADD (Objcookie); }

(2) b.aspx

String myname1value;myname1value = request.cookies["MyCookie"]. Value;

  Iv. Application

The Application object is scoped to the whole global, which means it is valid for all users. It is valid throughout the application life cycle, similar to using global variables, so it can be accessed from different pages. It differs from the session variable in that the former is a global variable shared by all users, and the latter is a unique global variable for each user.

One might ask, since all users can use the application variable, where can he use it? Here's an example: the number of site visits. It can be manipulated when multiple requests are accessed.

  Pros : 1. Easy to use, consumes less server resources.

2. Not only can pass the simple data, but also can pass the object.

3. The size of the data volume is unlimited.

  cons : 1. It is easy to be manipulated as a global variable. Therefore, the variables used by individual users are generally not application.

How to use: 1. Create the name and value you need to pass in the code of the source page construct the application variable: application["Nmae"]= "Value (Or Object)";

2. The code on the destination page uses the application variable to remove the passed value. Result = application["Nmae"]

  Note : The common Lock and unlock methods are used to lock and unlock, in order to prevent concurrent modifications.

Example: (1) a.aspx

private void Button1_Click (object sender, System.EventArgs e) {application["name"] = Label1.Text;}

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {string name;   Application.Lock (); Name = application["Name"].   ToString (); Application.UnLock (); }

  Wu, Server.Transfer

  This can be said to be the method used by the development of the face-like object, which uses the Server.Transfer method to direct the process from the current page to another page, the new page uses the reply stream of the previous page, so this method is completely object-like, 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 outputs, and in the new page through Context.Handler to get the values of the various data types passed by the previous page, form data, QueryString. Because the redirection is complete on the server side, the URL address in the client browser does not change. When Server.Transfer is called, the current ASPX page terminates execution and the execution process goes 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 will not change, and the URL address in the client browser will change as the client completes and the new page processing request is made to the server side.
(2) The Server.Transfer is done on the server side, without requiring the client to make requests, reducing the client's request to the server side. [2]
(3) Server.Transfer can only jump to the page specified in the local virtual directory, that 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 only use the URL with parameters or combined with the above four ways to upload various types of values to the new page.

  advantages : 1. Direct in the server end multiplicity orientation, easy to use, reduce the client to the server side request.

2. You can pass values for various data types and values for controls.

  cons : 1. The URL address in the client browser is not changed, which can cause some unexpected problems in the new page. For example, if the source page and the destination page are not in the same virtual directory or its subdirectories, the use of relative path of the picture, hyperlink will lead to an error point.

How to use: 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. On the destination page, use Context.Handler to receive data: formerpage formerpage = (formerpage) Context.Handler; Then use the properties and methods of the formerpage to get the value of the previous page, or use the context.items["Myparameter" directly)

Example: (1) a.aspx

public string Name {get{return label1.text;} private void Button1_Click (object sender, System.EventArgs e) {Serve R.transfer ("b.aspx"); }

(2) b.aspx

private void Page_Load (object sender, EventArgs e) {a newweb;//instance a form Newweb = (source) Context.Handler;   String name; name = Newweb.name; }

The above is commonly used in several methods of page transfer, I generally use the session and querystring to pass the value, a few cases will be used to cookies. This article is only to introduce the use of these methods, the internal principle does not have too much explanation, about the session storage method see: Session storage Mode and configuration

  

Reference article: 1, ASP. NET pages, and several ways to pass values between

2, ASP. NET pages to pass values between the several methods

Asp. NET pages, and several ways to pass values between

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.