Asp. NET in the transfer of values between the several ways of finishing _ practical skills

Source: Internet
Author: User
Tags server memory
But generally speaking, the commonly used simpler has the querystring,session,cookies,application,server.transfer.
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 address of the browser.
2. Cannot pass object.
Usage: 1. The URL address is constructed using the name and value to be passed in the code on the source page.
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
Copy Code code as follows:

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
Copy Code code as follows:

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

   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 not limited.
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 construct 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
Copy Code code as follows:

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

(2) b.aspx
Copy Code code as follows:

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

   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 privacy.
2. Security is not high, easy to forge.
  
How to use:
1. Create the name and value you want to pass on the code in the source page construct the cookie object:
HttpCookie Objcookie = new HttpCookie ("MyCookie", "hello,cookie!");
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
Copy Code code as follows:

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

(2) b.aspx
Copy Code code as follows:

String Myname1value;
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. Simple to use, less server resources.
2. Not only can pass the simple data, but also can pass the object.
3. The amount of data is not limited.
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
Copy Code code as follows:

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

(2) b.aspx
Copy Code code as follows:

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

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. [2]
(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.
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. 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
Copy Code code as follows:

public string Name
{
get{return Label1.Text;}
}
private void Button1_Click (object sender, System.EventArgs e)
{
Server.Transfer ("b.aspx");
}

(2) b.aspx
Copy Code code as follows:

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 pages to pass the value of the method, I generally use 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 is not too much explanation, about the memory of the session see: Session storage Mode and configuration
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.