Parameter transfer method between Web pages

Source: Internet
Author: User
In the WEB environment, data transmission between pages can be passed through the traditional URL parameter transmission and session-level transmission, and the page value can be transferred through the ASP. NET Server Control.

1 use Querystring

Querystring is a simple way to pass parameters. Its disadvantage is that it displays the value to be transmitted in the address bar of the browser (Insecure ), at the same time, objects cannot be passed when querystring is used for data transmission. The parameter length cannot exceed 1024 bytes. When the parameter has Chinese characters, HttpUlitity is usually used. is the UrlEncode method used to encode and decode the parameters. Of course, if you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the parameter transfer process.

A. aspx C # code

Private void button#click (ob Ject sender System. EventArgs e)

{

String s_url;

S_url = "B. aspx? Name = "+ Label1.Text;

Response. Redirect (s_url );

}

B. C # code in aspx

Private void Page_Load (ob Ject sender EventArgs e)

{

Label2.Text = Request. QueryString ["name"];

}

2. Use Application to pass Parameters

Passing Parameters Using Application variables is the most common method. The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock. However, the disadvantage is that the use of applications is generally relatively static compared to the entire project, such as database connection variables. For each user, the variables in each session may not be the same. Generally, the value of the Application variable is specified at the end of Config. You can also specify it in global. ascx. For example:

A. aspx C # code

Private void button#click (ob Ject sender System. EventArgs e)

{

Application ["name"] = Label1.Text;

Server. Transfer ("B. aspx ");

}

B. C # code in aspx

Private void Page_Load (ob Ject sender EventArgs e)

{

String name;

Application. Lock ();

Name = Application ["name"]. ToString ();

Application. UnLock ();

}

3. Use Session Variables

Using Session variables to pass values is the most common method. This method not only transfers values to the next page, but also transmits values to multiple pages, the variable does not disappear until the value of the Session variable is removed. However, the use of Session variables often occupies the server's memory, so when the webpage traffic is heavy, the Session will not be able to complete the worthwhile transfer task well, because the session has a timeout, therefore, user operations are also affected. For example:

Create a web form

Put a button1 in the new web form, and place two TextBox1TextBox2 to create a click event for the button. The Code is as follows:

Private void button#click (ob Ject sender System. EventArgs e)

{

Session ["name"] = TextBox1.Text;

Session ["email"] = TextBox2.Text;

Response. Redirect ("webform2.aspx ");

}

Create a new target page named webform2. place two Label1Label2 in webform2 and add the following code in Page_Load of webform2:

Private void Page_Load (ob Ject sender System. EventArgs e)

{

Label1.Text = Session ["name"]. ToString ();

Label2.Text = Session ["email"]. ToString ();

Session. Remove ("name ");

Session. Remove ("email ");

}

Run the command to view the passed result.

4. PASS Parameters Using the cookie Method

This is also a common method. Cookies are text files on the browser. By reading and writing them, you can easily implement the same process between pages and sessions, it is for every user, but there is a fundamental difference, that is, the Cookie is stored on the client, and the session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request. Cookie is a text file on the browser. By reading and writing it, you can easily transfer parameters between pages. It also serves as the storage time, that is, the validity period of data, it is indeed the best saved. However, the disadvantage is poor security, limited size, and the object cannot be saved. For example:

A. aspx C # code

Private void button#click (ob Ject sender System. EventArgs e)

{

HttpCookie cookie_name = new HttpCookie ("name ");

Cookie_name.Value = Label1.Text;

Reponse. AppendCookie (cookie_name );

Server. Transfer ("B. aspx ");

}

B. C # code in aspx

Private void Page_Load (ob Ject sender EventArgs e)

{

String name;

Name = Request. Cookie ["name"]. Value. ToString ();

}

5. Use the Server. Transfer Method

This can be said to be the method used for face object development. It uses Server. the Transfer method directs the process from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is completely object-like and concise and effective. It is highly efficient to directly redirect data on the server. It can also pass the submission value of the previous page. For example, if the value of page A is submitted to page B, and the value of page B is transferred to Page C, Page C can also receive the value submitted by page. However, its disadvantage is that it cannot refresh the page. For example, If logon information is submitted to page B on page A and Transfer is sent to page A after page B is processed, page A cannot be refreshed, and the specified expiration does not work. If pages A and B are not in the same directory, we will find more unexpected results. The images and hyperlinks with relative links have all changed, this is because page B reads the content of page A but outputs it as page B, so the path is changed. For example:

A. aspx C # code

Public string Name

{

Get {return Label1.Text ;}

}

Private void button#click (ob Ject sender System. EventArgs e)

{

Server. Transfer ("B. aspx ");

}

B. C # code in aspx

Private void Page_Load (ob Ject sender EventArgs e)

{

A newWeb; // instance a form

NewWeb = (source) Context. Handler;

String name;

Name = newWeb. Name;

}

The five methods for passing values between Web pages have their own advantages and disadvantages. We can find their respective characteristics through their advantages and disadvantages. Therefore, when using them, you must select the method based on the actual situation.

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.