Asp.net| Tips | page
Today, someone in the TM group asked about the way the page was passed, and sparked a discussion. It seems that there are a lot of people concerned, so I made a summary of my personal point of view, I hope to help.
1. Using QueryString variables
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. Here is an example:
A.aspx's C # code
private void Button1_Click (object sender, System.EventArgs e)
{
String S_url;
S_url = "B.aspx?name=" + Label1.Text;
Response.Redirect (S_url);
}
C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
Label2.Text = request.querystring["name"];
}
2. Using Application Object variables
The scope of the Application object is the entire global, which means it works for all users. Its commonly used methods are lock and unlock.
A.aspx's C # code
private void Button1_Click (object sender, System.EventArgs e)
{
application["name"] = Label1.Text;
Server.Transfer ("b.aspx");
}
C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
String name;
Application.Lock ();
Name = application["Name"]. ToString ();
Application.UnLock ();
}
3. Use Session Variables
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.
A.aspx's C # code
private void Button1_Click (object sender, System.EventArgs e)
{
session["name"] = Label.text;
}
C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
String name;
Name = session["Name"]. ToString ();
}
4. Use Cookie object variables
This is also common use of the method, as with the session, what it is for each user, but there is an essential difference, that is, cookies are stored in 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.
A.aspx's C # code
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");
}
C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
String name;
Name = request.cookie["Name"]. Value.tostring ();
}
5. Using the Server.Transfer method
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.
A.aspx's C # code
public string Name
{
get{return Label1.Text;}
}
private void Button1_Click (object sender, System.EventArgs e)
{
Server.Transfer ("b.aspx");
}
C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
a newweb; Instance a form
Newweb = (source) Context.Handler;
String name;
name = Newweb.name;
}