Some common methods of transferring parameters between pages in. Net

Source: Internet
Author: User
Tags server memory

Transferred from : http://www.cnblogs.com/lxshanye/archive/2013/04/11/3014207.html

Reference : http://www.cnblogs.com/zhangkai2237/archive/2012/05/06/2486462.html

1. Using the QueryString variable
QueryString is a very simple method of transmitting values that can be displayed in the browser's address bar. You can use this method if you are passing one or more security requirements that are not high or have a simple structure. However, you cannot use this method for passing arrays or objects.

Here is an example:
C # code for 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);
}

C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
Label2.Text = request.querystring["name"];
}

Pros: 1. Simple to use, it is very effective to pass a number or text value when the security requirements are not high.

Cons: 1. Lack of security, because its value is exposed in the browser's URL address.

2. The object cannot be passed.

Problems that may occur:

1. When dealing with the resonse.querystring function of Chinese character parameter passing, there is an error that can not pass the concrete value of the parameter completely, the solution has two methods:
Method One: You need to reset the encoding and globalization settings in Web. config.
1, the first line: <?xml version= "1.0" encoding= "Utf-8"?> changed to: <?xml version= "1.0" encoding= "GB2312"?>

2, <!--globalization This section sets the globalization settings for the application. --><globalizationrequestencoding= "Utf-8" responseencoding= "Utf-8"/> Change to:<!--Globalization This section sets the globalization settings for the application. --><globalizationrequestencoding= "GB2312" responseencoding= "GB2312"/>
Method Two: Encode and decode Chinese characters or special characters using Server.URLEncode and Server.urldecode.


2. Using the Application object variable
The Application object is scoped to the whole global, which means it is valid for all users. Its common method is to use lock and unlock.

The application variable is valid throughout the application life cycle, similar to using global variables, 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 unique global variable for each user. For example, the counter variable for website access is generally used as a application variable, which can be manipulated when multiple requests are accessed, and is used directly by each page of the application. User login account name generally use session variable, multiple requests access to have their own session variable, only to their own session variables to operate, the entire application of the various pages directly use this variable to obtain the user's basic information.

C # code for a.aspx
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 ();
}
Pros: 1. Easy to use, consumes less server resources.

2. Not only can pass the simple data, but also can pass the object.

3. The size of the data volume is unlimited.
Cons:
1. It is easy to be manipulated as a global variable.

3. Use the session variable
Presumably this is the most common usage of everyone, its operation is similar to application, acting on the user's personal, so, excessive storage will lead to the depletion of server memory resources.
C # code for a.aspx
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 ();
}

Pros: 1. Simple to use, not only to pass simple data types, but also to pass objects. 2. The size of the data volume is unlimited.
Cons:
1. Storing large amounts of data in session variables consumes more server resources.

4. Using Cookie Object variables
This is also a common use of the method, as in the session, what it is for each user, but there is an essential difference, that is, the cookie is 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.
Cookies are used to store small pieces of information in the user's browser, to save information about the user, such as the user's ID when accessing a website, the user's preferences, etc., and the user can retrieve the previous information through the next visit. So cookies can also pass values between pages. Cookies are passed back and forth between the browser and the server via an HTTP header. A cookie can contain only the value of a string, and if you want to store an integer value in a cookie, you need to first convert to a string. All browser cookies can be obtained by traversing the cookie collection of the Request object.
C # code for a.aspx
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 ();
}
Advantages: 1. Direct in the server end multiplicity orientation, easy to use, reduce the client to the server side request. 2. You can pass values for various data types and values for controls.
Cons: 1. The URL address in the client browser is not changed, which can 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 its subdirectories, the use of relative path of the picture, hyperlink will lead to an error point.

5. Using the Server.Transfer method
This can be said to be the method used by the development of the face-like object, which uses the Server.Transfer method to direct the process from the current page to another page, the new page uses the reply stream of the previous page, so this method is completely object-like, concise and effective.

This method is slightly more complex than the method described above, but is particularly useful in the transfer of values between pages, using this method you can access the exposed values in another page as object properties, of course, using this method, you need to write some additional code to create some properties so that you can access it on another page, However, the benefits of this approach are also obvious. In general, using this approach is concise and object-oriented.

The whole process of using this method is as follows:

1. Add the necessary controls to the page

2. Create a Get property procedure that returns a value

3. Create a button and link button that can return the form

4. Call the Server.Transfer method in the button click event handler to transfer to the specified page

5. On the second page, we can use the Context.Handler property to obtain a reference to the previous page instance object, through which you can use the value of the control that accesses the previous page


C # code for a.aspx
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)
{
WebForm1 Newweb; Create instance of source Web Form

Newweb = (WebForm1) Context.Handler;
String name;
name = Newweb.name;
}

Some common methods of transferring parameters between pages in. Net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.