Several methods for transferring values between ASP. NET pages session storage and Configuration

Source: Internet
Author: User

Link: http://www.cnblogs.com/zhangkai2237/archive/2012/05/06/2486462.html

Page value passing is a problem that will be faced in the early stages of learning Asp.net. In general, there are page value passing, storing object Value passing, Ajax, class, model, form, and so on. However, in general, querystring, session, cookies, application, server. Transfer are commonly used.

 

  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. Objects cannot be passed.

Method of use: 1. Use the name and value to be passed in the Code on the Source Page to construct the URL address.
2. Use response. Redirect (URL); to redirect the code on the source page to the above URL.
3. Use request. querystring ["name"]; In the code on the target page to retrieve 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"]; }

 

  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. The data size is unlimited.

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 to clear 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(); }

 

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 are often criticized for being used to collect user privacy.

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:

HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");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

private void Button1_Click(object sender, System.EventArgs e){  HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");  Response.Cookies.Add(objCookie); }

(2) B. aspx

string myName1Value;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. The data size is unlimited.

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

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(); }

 

  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. [2]
(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, which is easy to use and reduces the number of requests sent from the client to the server.

2. Various data types and control values can be passed.

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.

Method of use: 1. In the code on the Source Page, use the server. Transfer of the page class to jump to another page and pass the page data: Server. Transfer ("B. aspx", "false ").

2. on the target page, use context. handler to receive data: 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

public string Name {   get{ return Label1.Text;} } private void Button1_Click(object sender, System.EventArgs e) {   Server.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 ;}

 

These are commonly used methods for transferring values between pages. I generally use session and string. querystring to transfer values. In rare cases, cookies are used. This article only introduces how to use these methods, and does not explain the Internal principles too much. For more information about session storage methods, see session storage methods and configuration.

  

Reference article: 1. Several Methods for passing values between ASP. NET pages

2. Several Methods for passing values between ASP. NET pages

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.