Asp. NET page transfer value (4): Server.Transfer

Source: Internet
Author: User

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.

Server.Transfer is to go from the current ASPX page to the new ASPX page, the server side executes the new page and outputs, and in the new page through Context.Handler to get the values of the various data types passed by the previous page, form data, QueryString. Because the redirection is complete on the server side, the URL address in the client browser does not change. When Server.Transfer is called, the current ASPX page terminates execution and the execution process goes to another ASPX page, but the new ASPX page still uses the reply stream created by the previous ASPX page.

  PS: Compare the difference between Server.Transfer and Response.Redirect.
(1) Server.Transfer is done on the server side, so the URL address in the client browser will not change, and the URL address in the client browser will change as the client completes and the new page processing request is made to the server side.
(2) The Server.Transfer is done on the server side, without requiring the client to make requests, reducing the client's request to the server side. [2]
(3) Server.Transfer can only jump to the page specified in the local virtual directory, that 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 only use the URL with parameters or combined with the above four ways to upload various types of values to the new page.

  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.

How to use: 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 ("B.aspx", "false").

2. On the destination page, use Context.Handler to receive data: formerpage formerpage = (formerpage) Context.Handler; Then use the properties and methods of the formerpage to get the value of the previous page, or use the context.items["Myparameter" directly)

Indextest.aspx Background page:

1  Public stringName2 { 3     Get{returnLabel1.Text;} 4 } 5 Private voidButton1_Click (Objectsender, System.EventArgs e)6 { 7Server.Transfer ("indextestlist.aspx"); 8}

Indextestlist.aspx Background page:

1 Private void Page_Load (object23//4 newweb =   5string6 name =7 }   

The above is commonly used in several pages of the method of passing values, generally use the session and querystring to pass the value, a few cases will be used to cookies.

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.