An introduction to how the ASP.NET page passes values

  • 2020-05-19 04:30:05
  • OfStack

1. QueryString

QueryString is a very simple way to send a value and display it in the browser's address bar. You can use this method if you are passing one or more values with low security requirements or simple structure. But you can't use this method for passing arrays or objects.

Advantages of this approach: 1. Simple to use and very effective for passing numeric or text values when 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. 2. Objects cannot be passed.

Use method: 1. Construct the URL address in the code of the source page with the name and value to be passed; 2. 2. Use Response.Redirect (URL) for the code on the source page; Redirect to the URL address above; 3. Use Request.QueryString ["name"] for the code on the destination page; Fetch the value passed in the URL address.

Example: (1) a aspx

1.private void Button1_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.}
2. Session

This must be one of the most common USES of Application, which is similar to Application and ACTS on the individual user. Therefore, excessive storage can lead to the depletion of server memory resources.

Advantages: 1. Easy to use, can not only transfer simple data types, but also transfer objects; 2. 2. There is no limit to the amount of data.

Disadvantages: 1. Storing a large amount of data in Session variable will consume more server resources; 2. 2. Easy to lose.

1. Create the name and value you need to pass in the source code to construct Session variable :Session["Name"]="Value(Or Object)"; 2. The code on the destination page USES the Session variable to fetch the passed value. Result = Session [" Nmae "]

Note: session can be destroyed when not in use by removing 1: Session.Remove ("session name "); Clear all: Session.Clear ();

Example: (1) a aspx

1.private void Button1_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 commonly used method. Cookie is used to store small pieces of information on the user's browser and save relevant information of the user, such as ID of the user when the user visits a certain website, user preferences, etc., so that the user can retrieve the previous information on the next visit. So Cookie can also pass values between pages. Cookie is passed back and forth between the browser and the server via the HTTP header. Cookie can only contain string values, and if you want to store integer values in Cookie, you need to convert them to a string first.

Like Session1, it is for every user, but there is an essential difference, that is, Cookie is stored on the client side, while session is stored on the server side. Moreover, Cookie should be used in conjunction with ASP.NET built-in object Request.

Advantages: 1. Easy to use, is a very common way to maintain user status. For example, it can be used to maintain user status when users cross multiple page forms in a shopping site.

Disadvantages: 1. It is often criticized for being used to collect users' privacy; 2. 2. Low security, easy to forge.

1. Create the name and value you need to pass in the source code to 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 the 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)
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;
4. Application

The Application object is globally scoped, which means it is valid for all users. It is valid throughout the application lifecycle, similar to using global variable 1, 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 global variable unique to each user.

One might ask, since all users can use the application variable, where can they use it? Here's an example: site visits. It can be manipulated for multiple requests.

Advantages: 1. Easy to use, less consumption of server resources; 2. 2. Can not only transfer simple data, but also transfer objects; 3. There is no limit to the amount of data.

Disadvantages: 1. As a global variable, it is easy to be manipulated by mistake. Therefore, the variable 1 used by a single user cannot be used by application.

Create the name and value you need to pass in the source code to construct Application :Application["Nmae"]="Value(Or Object)"; 2. The code on the destination page USES the Application variable to fetch the passed value. Result = Application [" Nmae "]

Note: the lock and unlock methods are commonly used for locking and unlocking to prevent concurrent modifications.

Example: (1) a aspx

1.private void Button1_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.}
5. Server.Transfer

Server.Transfer is used to guide the process from the current page to another page. The new page USES the reply flow from the previous page. Therefore, this method is completely object-oriented, concise and effective.

Server.Transfer goes from the current ASPX page to the new ASPX page. The server executes the new page and outputs it. In the new page, Context.Handler obtains the values of various data types, form data and QueryString passed from the previous page through Context.Handler. When Server.Transfer is called, the current ASPX page terminates execution and the execution process moves to another ASPX page, but the new ASPX page still USES the reply flow created by the previous 1ASPX page.

ps: compare Server.Transfer with Response.Redirect.

(1)Server.Transfer is completed on the server side, so the URL address in the client browser will not change; Response.Redirect is the client's completion of a new page processing request to the server, so the URL address in the client's browser will change.

(2)Server.Transfer is completed on the server side, which does not require the client to make a request, thus reducing the client's request to the server side.

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

(4) Server.Transfer can send various types of values from the previous page to the new page; Response.Redirect can only send various types of values to a new page by taking parameters in URL or by combining the above four methods.

Advantages: 1. Direct redirection at the server side, simple and convenient to use, reducing the client side's request to the server side; 2. 2. You can pass values of various data types and controls.

Disadvantages: 1. The URL address in the client browser is not changed, which may 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 subdirectory, then images and hyperlinks using relative paths will lead to the wrong direction.

1. In the code of the source page, use Server.Transfer of class Page to jump to another page and pass the page data: Server.Transfer (" b.aspx ","false"); 2. In the destination page, use Context.Handler to receive data: FormerPage formerPage = (FormerPage) Context.Handler; Then use the properties and methods of formerPage to get the value of the previous page, or Context.Items ["myParameter "]

Example: (1) a aspx

1.public string Name
2.{
3.get{ return Label1.Text;}
4.}
5.private void Button1_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 articles: