5 kinds of methods .net C # pass values between the six methods in C # page parameter passing by value

Six methods by value parameter passing between pages in .net C #

 

1.QueryString is a very simple way by value, he can transfer the value displayed in the address bar of your browser. If the transmission is a simple value or more or less demanding safety structure, this method may be used. But for passing an array or object, it can not be used in this method has. Below is an example:

Press Ctrl + C to copy the code
Press Ctrl + C to copy the code

query by value and classified post, get formats such as:

Copy the code
// post request 
String name = the Request [ "name"] toString ();. 
. Request.Form.Get String name = ( "name") toString (); 
// GET request 
string name = Request.QueryString [ "name" ] .toString (); 
but I found that no matter whether it is post and get passed by value are available 
string name = Request [ "name" ] toString ().;
Copy the code

Form submission get and post difference in the manner of points summarized as follows: 
1. get the data is acquired from the server, the server transmits data to a post. 
2. get the parameter is added to data queue ACTION attribute of a form submission URL referred to in the various fields and values within the form-one correspondence can be seen in the URL. post through HTTP post mechanism to various fields with its contents is placed in a form HTML HEADER URL address transmitted with the ACTION attribute refers. Users do not see this process. 
3. For get embodiment, a server Request.QueryString variable value acquisition for post embodiment, the server acquires data submitted by Request.Form. 
4. get the amount of data transfer is small, not greater than 2KB. post large amount of data transmission, typically default to unlimited. But in theory, IIS4 the maximum amount of 80KB, IIS5 for 100KB. 
5. get security is very low, post high security.

 

2. Using the Application object variable

  Scope is the entire global Application object, that is valid for all users. Lock with their usual methods and UnLock.

Copy the code
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
 {
     Application["name"] = Label1.Text;
     Server.Transfer("b.aspx");
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     string name;
     Application.Lock();
     name = Application["name"].ToString();
     Application.UnLock();
 }
Copy the code

3. Use Session Variables

  Presumably this is definitely you use the most common usage, and its operation is similar to the Application, acting on the individual user, so that excess storage can lead to depletion of server memory resources.

Copy the code
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
 {
     Session["name"] = Label.Text;
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     string name;
     name = Session["name"].ToString();
 }
Copy the code

4. Use object variable Cookie

  This is, we often use the method, and Session, as it is even for every user, but there is an essential difference, that Cookie is stored on the client, and the session is stored on the server side. And to tie in ASP.NET Cookie use built-in objects Request to use.

Copy the code
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
 {
     HttpCookie cookie_name = new HttpCookie("name");
     cookie_name.Value = Label1.Text;
     Reponse.AppendCookie(cookie_name);
     Server.Transfer("b.aspx");
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     string name;
     name = Request.Cookie["name"].Value.ToString();
 }
Copy the code

5. Method Server.Transfer

  This method can be said to be developed image object plane is used, the flow directed from the current page to another page using Server.Transfer method, before a new page using the page response to the flow, so this surface is completely like objects, simple and effective.

Copy the code
a.aspx的C#代码
public string Name
 {
     get{ return Label1.Text;}
 }
 private void Button1_Click(object sender, System.EventArgs e)
 {
     Server.Transfer("b.aspx");
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     a newWeb;   //实例a窗体
     newWeb = (source)Context.Handler;
     string name;
     name = newWeb.Name;
 }
Copy the code

 

Here are some advantages and disadvantages of traditional values

cookie
     ways: the presence of classical methods of client data.
     Disadvantages: low security, set limits by the client, a site remaining 20 cookie, each with a capacity of 4096 bytes.

Session
     mode: the user data is stored on the server.
     Features: asp.net session can be set storage, location, SessionID preservation is dependent cookie.
             You can store objects directly. 
     Disadvantage: ASP.NET have hidden failure
Cache
     manner: The user data stored in the data server cache. 
     Features: can greatly improve efficiency. You can store objects directly.
Appliction
     manner: storing data thereto, corresponding to the global variable. 
     Features: You can store objects directly. Sharing data across sites
ViewState
     way: asp.net unique mechanism, to restore the status page. 
     Features: The page for each control and its stored data serialization name for the existence of _ViewState hidden field.
     Disadvantages: there is HTML, less secure. You can set encryption and authentication, but the amount of data will increase significantly, affect efficiency.
Static
     mode: the data is stored in a static variable. 
     Features: help to improve efficiency.
     Cons: If used properly will cause the user data between pages or disorder, cause great risk. Recommended assignment only once, absolute prohibition for a single user to change this value.

1.QueryString is a very simple way by value, he can transfer the value displayed in the address bar of your browser. If the transmission is a simple value or more or less demanding safety structure, this method may be used. But for passing an array or object, it can not be used in this method has. Below is an example:

Press Ctrl + C to copy the code
Press Ctrl + C to copy the code

query by value and classified post, get formats such as:

Copy the code
// post request 
String name = the Request [ "name"] toString ();. 
. Request.Form.Get String name = ( "name") toString (); 
// GET request 
string name = Request.QueryString [ "name" ] .toString (); 
but I found that no matter whether it is post and get passed by value are available 
string name = Request [ "name" ] toString ().;
Copy the code

Form submission get and post difference in the manner of points summarized as follows: 
1. get the data is acquired from the server, the server transmits data to a post. 
2. get the parameter is added to data queue ACTION attribute of a form submission URL referred to in the various fields and values within the form-one correspondence can be seen in the URL. post through HTTP post mechanism to various fields with its contents is placed in a form HTML HEADER URL address transmitted with the ACTION attribute refers. Users do not see this process. 
3. For get embodiment, a server Request.QueryString variable value acquisition for post embodiment, the server acquires data submitted by Request.Form. 
4. get the amount of data transfer is small, not greater than 2KB. post large amount of data transmission, typically default to unlimited. But in theory, IIS4 the maximum amount of 80KB, IIS5 for 100KB. 
5. get security is very low, post high security.

 

2. Using the Application object variable

  Scope is the entire global Application object, that is valid for all users. Lock with their usual methods and UnLock.

Copy the code
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
 {
     Application["name"] = Label1.Text;
     Server.Transfer("b.aspx");
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     string name;
     Application.Lock();
     name = Application["name"].ToString();
     Application.UnLock();
 }
Copy the code

3. Use Session Variables

  Presumably this is definitely you use the most common usage, and its operation is similar to the Application, acting on the individual user, so that excess storage can lead to depletion of server memory resources.

Copy the code
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
 {
     Session["name"] = Label.Text;
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     string name;
     name = Session["name"].ToString();
 }
Copy the code

4. Use object variable Cookie

  This is, we often use the method, and Session, as it is even for every user, but there is an essential difference, that Cookie is stored on the client, and the session is stored on the server side. And to tie in ASP.NET Cookie use built-in objects Request to use.

Copy the code
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
 {
     HttpCookie cookie_name = new HttpCookie("name");
     cookie_name.Value = Label1.Text;
     Reponse.AppendCookie(cookie_name);
     Server.Transfer("b.aspx");
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     string name;
     name = Request.Cookie["name"].Value.ToString();
 }
Copy the code

5. Method Server.Transfer

  This method can be said to be developed image object plane is used, the flow directed from the current page to another page using Server.Transfer method, before a new page using the page response to the flow, so this surface is completely like objects, simple and effective.

Copy the code
a.aspx的C#代码
public string Name
 {
     get{ return Label1.Text;}
 }
 private void Button1_Click(object sender, System.EventArgs e)
 {
     Server.Transfer("b.aspx");
 }
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
 {
     a newWeb;   //实例a窗体
     newWeb = (source)Context.Handler;
     string name;
     name = newWeb.Name;
 }
Copy the code

 

Here are some advantages and disadvantages of traditional values

cookie
     ways: the presence of classical methods of client data.
     Disadvantages: low security, set limits by the client, a site remaining 20 cookie, each with a capacity of 4096 bytes.

Session
     mode: the user data is stored on the server.
     Features: asp.net session can be set storage, location, SessionID preservation is dependent cookie.
             You can store objects directly. 
     Disadvantage: ASP.NET have hidden failure
Cache
     manner: The user data stored in the data server cache. 
     Features: can greatly improve efficiency. You can store objects directly.
Appliction
     manner: storing data thereto, corresponding to the global variable. 
     Features: You can store objects directly. Sharing data across sites
ViewState
     way: asp.net unique mechanism, to restore the status page. 
     Features: The page for each control and its stored data serialization name for the existence of _ViewState hidden field.
     Disadvantages: there is HTML, less secure. You can set encryption and authentication, but the amount of data will increase significantly, affect efficiency.
Static
     mode: the data is stored in a static variable. 
     Features: help to improve efficiency.
     Cons: If used properly will cause the user data between pages or disorder, cause great risk. Recommended assignment only once, absolute prohibition for a single user to change this value.

Guess you like

Origin www.cnblogs.com/yinjinli/p/12169553.html