Several ways of passing values between pages in ASP.NET

  • 2020-05-17 05:12:54
  • OfStack

But 1 in general, the more common ones are QueryString, Session, Cookies, Application, Server.Transfer.
1. QueryString
QueryString is a very simple way to send a value that is displayed 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. Objects cannot be passed.
How to use: 1. Construct the URL address in the source page code with the name and value to be passed.
2. Use Response.Redirect (URL) for the code on the source page; Redirect to the URL address above.
3. Use Request.QueryString ["name"] in the code on the destination page; Fetch 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"]; 
} 

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. Simple to use, not only can pass simple data types, but also can pass objects.
2. There is no limit to the amount of data.
Disadvantages: 1. Storing a large amount of data in the Session variable consumes more server resources.
2. Easy to lose.
1. Create the name and value you need to pass in the source code to construct the 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: 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 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 the user's ID when the user visits a certain website, the user's preference, 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. If you want to store integer values in Cookie, you need to convert them to a string.
Like Session1, it is for every user, but there is an essential difference that Cookie is stored on the client side and 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. Low security, easy to forge.

Usage:
1. Create the name and value you need to pass in the source code to construct the Cookie object:
HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");
Response.Cookies.Add(cookie);
2. In the code of the destination page, Cookie object is used to fetch the passed value: Result = Request. Cookies["myCookie"].
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; 

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. Can pass not only simple data, but also 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.
Use method: 1. Create the name and value you need to pass in the source code to construct Application variable :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
 
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(); 
} 

5. Server.Transfer
This can be said to be the method used in the development of the surface object, which USES the Server.Transfer method to guide the process from the current page to another page. The new page USES the reply flow of the previous page.
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 is used to obtain the values of various data types, form data and QueryString passed from the previous page. 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 done by the client side, which makes a new page processing request to the server side, so the URL address in the client browser will change.
(2)Server.Transfer is completed on the server side, which does not require the client to make a request, which reduces the client to make a request to the server side. [2]
(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 of 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's request to the server side.
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.
Use method: 1. In the source page code, use Page class Server.Transfer to jump to another page and pass the page data: Server.Transfer (" b.aspx ","false").
2. In the destination page, Context.Handler is used 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 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; // The instance a The form  
  newWeb = (source)Context.Handler; 
  string name; 
  name = newWeb.Name; 
} 

The above are some common methods of transferring values between pages. I usually use session and querystring to transfer values, and cookie is used in a few cases. This article only introduces the usage of these methods without much explanation of the internal principles. For the storage mode of session, please refer to the storage mode and configuration of session

Related articles: