Get started with asp.net by passing parameter variable code between pages

Source: Internet
Author: User
Tags time limit advantage

1. QueryString []

Advantage: it is easy to use and highly efficient to transmit numbers and text values when security requirements are not high.

Disadvantage: The security is not high, the parameter value is directly exposed in the Url, the transfer size is limited, and the object cannot be transferred.

Usage: construct the Url in page A: string url = "B. asp tutorial x? ID = 1 & name = 'hangsan' "Response. Redirect (url );

Accept string id = Request. QueryString ["ID"] on page B;

The response redirection method is the simplest way to redirect a webpage to another webpage. When the Web server receives a redirection request, it sends a response header to the client, which causes the client to send a new request to the server. In other words, a redirection request is actually two request responses: one is the initial request response, and the other is the new redirect request response.

It is easy to implement redirection in ASP. NET. The following code demonstrates how to use Response. Redirect to Redirect a webpage:

Protected void Redirect_Click (object sender, EventArgs e)
{
Response. Redirect ("menu. aspx ");
}

Note that the redirect request is only a GET request, which means that we cannot submit data from the source page through the redirection command. However, we can use a query string to transmit data in redirection. As shown in the following code:


Protected void Redirect_Click (object sender, EventArgs e)
{
Response. Redirect ("menu. aspx? UserName = "+ UserName. Text ));
      }

In the preceding example, a query string is passed as a parameter to the target URL of the Response. Redirect Method. We can use the following code to obtain the source data.


Protected void Page_Load (object sender, EventArgs e)
{
String userName = Request ["userName"];
      }


2. Session

Advantage: simple to use, not only can pass simple data types, but also can pass objects. There is no limit on the data size.

Disadvantage: storing large amounts of data will consume a lot of server resources.

Usage: in the code of page A, Session ["name"] = "zhang san"; in page B, string name = Session ["name"];

3. Application

Advantage: simple to use, consume less server resources, not only can transmit simple data, but also can transmit objects, the size of data is not limited.

Disadvantage: as a global variable, it is prone to misoperations.

Usage: same as Session.

4. Cookie

Advantage: it is easy to use and is a very common method to maintain the user status. It is stored in the client browser and does not occupy server resources.

Disadvantage: the client has a time limit and is out of date. Only string values can be saved.

Usage: Stored Value: HttpCookie cookie = new HttpCookie ("myCookie", "this is Cookie Value ");

Response. Cookies. Add (cookie );

Value: string Value = Request. Cookies ["myCookie"]. value;

 

5. Server. Transfer

When Server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to the new page. However, the response stream created on the previous page is used.

Compare Server. Transfer and Response. Redirect:

1. Server. Transfer is completed on the Server, so the Url address in the browser on the client will not be changed to the new page address to jump. Response. Redriect is completed by the client. When a new page processing request is submitted to the server, the client Url will change. Therefore, Server. Transfer reduces the number of requests sent from the client to the Server.

2. Server. Transfer can only jump to the page specified by the local virtual directory, that is, the page in the project, while Response. Redirect is very flexible and can jump to any URL address.

3. Server. Transfer can pass values of the previous page to the new page. Response. Redirect can only use parameters in the URL.
The Server. Transfer method can also save the HttpContext of the initial page. Therefore, the value of the source page can be accessed on the target page. We can use the FormsCollection attribute to obtain the value of the source page from the target page. First, make sure that we use the overloaded method. This method has two parameters: the target URL and a Boolean value, telling the server whether to save the Form used to describe the source page value. The following code is used:

Server. Transfer ("Menu. aspx", true );
Then, the code for getting the value of a Textbox control named txtUserName on the target page is as follows:
Object obj = Request. Form ["txtUserName"];

 

 

Advantage: direct server-side redirection is easy to use and reduces client-side requests to the server. Data values of various types and page control values can be passed.

Disadvantage: the address in the client browser remains unchanged, which may lead to unexpected situations, such as the image URL path...

Usage: Server. Transfer ("B. aspx", false) on page );

On page B, you can obtain the object A a = (A) Context. handler; this is a object that can be used to obtain the value of the previous page, or directly use Context. items ["ItemName.

Note: You can use this method to obtain the value of the previous page only when loading for the first time. It cannot be obtained in the future Postback. Therefore, the value method should be placed in if (!) of Page_Load (! IsPostBack)

Related Article

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.