I. Use of QueryString
Using querystring to pass values between pages is a very common method that we often use in ASP.
(1) Advantages and disadvantages
Advantages:
1. Simple to use, it is very effective to pass numbers or text values when security requirements are low.
Disadvantages:
1. Lack of security because its value is exposed in the URL address of the browser.
2. Cannot pass object.
(2) Use method
1. The code in the source page constructs the URL address in the name and value that you want to pass.
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.
(3) Application examples
1. Source page *.aspx Code:
private void Button1_Click (object sender, System.EventArgs e)
{
String urladdress;
String Name1;
String Name2;
String Name3;
String name1value = "HelloName1";
int name2value = 1234567;
string name3value = "Hello name 3";
urladdress = "Destinationwebform.aspx?" Name1= "+ Name1value +" & "+" name2= "+ name2value.tostring () +" & "+" name3= "+ name3value;
Response.Redirect (urladdress);
}
2. Purpose page destinationwebform.aspx code:
private void Page_Load (object sender, System.EventArgs e)
{
String Myname1value;
int myname2value;
String Myname3value;
Myname1value = request.querystring["Name1"];
Myname2value = Convert.ToInt32 (request.querystring["Name2"));
Myname3value = request.querystring["Name3"];
}
(4) Problems that may arise
1 in processing the resonse.querystring function of Chinese characters, the error of the specific value of the parameter cannot be completely passed, two methods are solved.
Method One: You need to reset the encoding and globalization settings in Web.config.
1, first line: <?xml version= "1.0" encoding= "Utf-8"
Change to:
<?xml version= "1.0" encoding= "GB2312"
2 , <!-- Globalization
This section sets the globalization settings for the application.
<globalization
requestencoding= "Utf-8"
responseencoding= "Utf-8"
/>
Change to:
<!-- globalization
This section sets the globalization settings for the application.
<globalization
requestencoding= "GB2312"
responseencoding= "GB2312"
/>
[1]
Method Two: Use Server.URLEncode and Server.urldecode to encode and decode Chinese characters or special characters.
Ii. Use of Application variables
Using the application variable is the second way to pass a value between pages.
The application variable 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.
Give an example to explain:
Web site access to the counter variables commonly used application variable, multiple requests to share this variable, you can operate on it, the variable can be used throughout the application of the various pages directly.
User login account name general use session variable, multiple requests to have their own session variables, only to their own session variables to operate, the entire application of each page directly use this variable to get the user's basic information.
(1) Advantages and disadvantages
Advantages:
1. Easy to use and consumes 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 operate incorrectly.
(2) Use method
1. Create the name and value you want to pass in the code of the source page construct the 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"]
(3) Application examples
1. Source page *.aspx Code:
private void Button1_Click (object sender, System.EventArgs e)
{
String name1value = "HelloName1";
int name2value = 1234567;
application["Name1"] = Name1value;
application["Name2"] = Name2value;
}
2. Purpose page *.aspx code:
private void Page_Load (object sender, System.EventArgs e)
{
String Myname1value;
int myname2value;
Myname1value = application["Name1"]. ToString ();
Myname2value = (int) application["Name2"];
}
third, the use of Session variables
Using the application variable is the third way to pass a value between pages. The session variable is very similar to the application variable, and the difference is already mentioned in the above about the application variable.
(1) Advantages and disadvantages
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 a session variable consumes more server resources.
(2) Use method
1. Create the name and value you want to pass in the code of the source page construct the session variable: session["Nmae"]= "value (Or Object)";
2. The code on the destination page uses the session variable to take out the passed value. result = session["Nmae"]
(3) Application examples
Similar to the application variable, just replace the application with the session.
Four, use cookie object
Using a Cookie object is the fourth way to pass a value between pages. Cookies are used to store small chunks of information in a user's browser, to save information about a user, such as the user's ID, user's preference, and so on, and the next time the user accesses it, it can retrieve 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.
All of the browser's cookies can be obtained by traversing the cookie collection of the Request object. The method is as follows:
foreach (String strkey in Request.Cookies)
{
Lblcookies.text + = strkey + "=" + request.cookies[strkey]. Value;
}
(1) Advantages and disadvantages
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) Use method
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;
(3) Application examples
1. Source page *.aspx Code:
private void Button1_Click (object sender, System.EventArgs e)
{
HttpCookie Objcookie = new HttpCookie ("MyCookie", "hello,cookie!");
RESPONSE.COOKIES.ADD (Objcookie);
}
2. Purpose page *.aspx code:
private void Page_Load (object sender, System.EventArgs e)
{
String Myname1value;
Myname1value = request.cookies["MyCookie"]. Value;
}
v. Use of Server.Transfer
Using the Server.Transfer variable is the fifth way to pass a value between pages. The above four methods are often used in ASP, but this method is emerging in asp.net. 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. [2]
Compare the difference between Server.Transfer and the Response.Redirect used in "one" here.
(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.
Continue with our Server.Transfer usage.
(1) Advantages and disadvantages
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. [3]
(2) Use method
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 ("Destinationwebform.aspx", "false").
2. In the destination page, use Context.Handler to receive data:
Formerpage formerpage = (formerpage) Context.Handler;
Then use the Formerpage properties and methods to get the value of the previous page, or directly
context.items["Myparameter"]
To get the value of the previous page.
It is important to note that obtaining these values must be the first time a new page is loaded to properly get the various data types of the previous page or the value of the control. At a later postback, you cannot get the various data types of the previous page or the value of the control, because you get an instance of the current page at this time. So you need to use if (!) in the Page_Load () event of the new page (destinationwebform.aspx). IsPostBack) to get the value of the previous page to include the code to get the previous page to pass the various data types of values, form data, querystring.
(3) Application examples
1. Source page formerpage.aspx Code:
public string Hellocontextattribute
{
Get
{
Return to "Use Attribute:hello,context";
}
}
public string Hellocontextmethod ()
{
Return to "Call method:hello,context!";
}
public string Textboxvalue
{
Get
{
return TextBox1.Text;
}
}
private void Button1_Click (object sender, System.EventArgs e)
{
String name1value = "Hello, name1!";
TextBox1.Text = "hello,textbox1!";
ArrayList myList = new ArrayList (3);//Create dynamic array
Mylist.add ("hello,array1!"); /Add a new value to the dynamic array
Mylist.add ("hello,array2!");
Mylist.add ("hello,array3!");
The context can hold any data type, and the context dictionary is specific to an HTTP request.
For different clients, the values are not the same.
context.items["destinationlist"] = mylist;//Save a dynamic array in Context.items
CONTEXT.ITEMS.ADD ("Newcontext", "Hello,newcontext");//Save a set of name-value data in Context.items
Server.Transfer The second argument, if true, indicates that the values of form and querrystring on this page continue to be valid on the new page.
Otherwise, the TextBox1 value cannot be obtained on the new page.
Server.Transfer ("Destinationwebform.aspx?") name1= "+ name1value,true);
}
2. Purpose page destinationwebform.aspx code:
private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Try
{
String Hellocontextattribute;
String Hellocontextmethod;
String Textboxvalue;
String Contextitemsvalue;
String querystring;
ArrayList Listresult;
Formerpage formerpage = (formerpage) Context.Handler;
Hellocontextattribute = formerpage.hellocontextattribute;//to get a value from a property defined in Formerpage
Hellocontextmethod = Formerpage.hellocontextmethod ();//Get value by method defined in Formerpage
Textboxvalue = formerpage.textboxvalue;//Returns the value of a text control through the Formerpage
The following method is used for the user to enter a value in the control to obtain, but in the program to direct TextBox1.Text assignment, then the following method gets
Null value, in which case you need to use the Fetch method above
Textboxvalue = request.form["TextBox1"];
Contextitemsvalue = context.items["Newcontext"]. ToString ()//Gets the value through the items in the Formerpage
Listresult = (ArrayList) context.items["Destinationlist"];//gets the object from the Items in the context in Formerpage, casts the type:
QueryString = request.querystring["Name1"];//gets the value through Formerpage's URL in querystring
}
Catch
{
Response.Write ("error!");
}
}
}
This article references:
[1] http://blog.cnbie.com/blog_52105.html
[2] Http://sadier.cnblogs.com/articles/48299.html
[3] http://digest.qq.com/cgi-bin/wenji_content?id=517581