Value Transfer Between Pages Server. Transfer

Source: Internet
Author: User
/// Page:. aspxpublic partial class A: system. web. UI. page {public string name {get {return button1.text;} protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {server. transfer ("B. aspx ");} protected void button2_click (Object sender, eventargs e) {arraylist list = new arraylist (3); list. add ("This List"); list. add ("is for"); Li St. add ("formerpage to see. "); context. items ["formerpagelist1"] = List; server. transfer ("B. aspx ");} internal string getdataset () {return". aspx method ";}} public partial class A: system. web. UI. page {public string name {get {return button1.text;} protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {server. transfer ("B. aspx ");} p Rotected void button2_click (Object sender, eventargs e) {arraylist list = new arraylist (3); list. add ("This List"); list. add ("is for"); list. add ("formerpage to see. "); context. items ["formerpagelist1"] = List; server. transfer ("B. aspx ");} internal string getdataset () {return". aspx method ";}}/// page: B. aspxpublic partial class B: system. web. UI. page {protected void page_load (Object sender, E Ventargs e) {If (! Ispostback) {A apage = (a) context. handler; response. write ("Get. text Value of the button on aspx: "+ apage. name + "<br/>"); try {A former = (a) context. handler; txtfromparentpage. TEXT = "get the public attributes of classname defined in a:" + former. name; // obtain the classname public attribute defined in a // obtain the arraylist: context added to the context dictionary in. items ["formerpagelist1"] // gets the contex dictionary item and forcibly converts the item type to arraylist = context. items ["formerpagelist1"] As arraylist; string DS = former. getdatas Et (); // call the getdataset () Public method response defined in. write (DS + "<br/>"); response. write (". in aspx, arraylist "+" <br/> "); For (INT I = 0; I <list. count; I ++) {response. write (list [I] + "<br/>") ;}} catch {response. write ("error get data from parent transfer page! ");}}}}

The path in the server. Transfer () Address Bar does not change, but the background process changes.

In addition, it is recommended that the server. transfer be the local directory or the subdirectory under the local directory. Do not go to the upper-level directory.

 

Most people use response. Redirect to direct users to another page, while others seem to prefer the mysterious server. Transfer. What is the difference between response. Redirect and server. transfer?

Response. Redirect simply sends a message to the browser, telling the browser to locate another page. You can use the followingCodeDirect the user to another page:

Response. Redirect ("webform2.aspx ")

Or

Response. Redirect ("http://www.zhisi.net /")

Server. Transfer also uses one statement to direct the user to another page, for example, server. Transfer ("webform2.aspx "). However, this statement has a series of unique advantages and disadvantages.

First, use server. transfer directs to another page to reserve server resources. By changing the server's "Focus" and transmission request, it will tell the browser to redirect, which means that you will not occupy a large number of HTTP requests, therefore, this can reduce the pressure on the server and make your server run faster.

However, please note that because "transfer" can only run between the same site on the same server end, you cannot use server. Transfer to redirect users to the site on another server. Only response. Redirect can redirect to a site other than the server.

Second, server. transfer retains the URL address of the browser. This is helpful for streamlined data input, but it also increases the debugging complexity.

Also, the server. transfer method has another parameter -- "preserveform ". If you set this parameter to true, for example, server. Transfer ("webform2.aspx", true), querystring and any form variables will be passed to the page you are located at the same time.

For example, webform1.aspx has a text box named textbox1, which is passed to webform2.aspx using preserveform to true. You can still use request. Form ("textbox1") to obtain the value of the text box.

This technology is useful for wizard-type multi-page input, but here you must note that when you use the preserveform parameter, Asp. net has a bug. Normally, an error occurs when attempting to pass the form or querystring value. See: http://support.microsoft.com/default.aspx? Id = KB; en-US; q2017920

The unofficial solution is to set the enableviewstatemac attribute to true on the target page you want to pass, and then set it back to false. This indicates that you need to use the false value of enableviewstatemac to solve this problem.

Conclusion: Response. Redirect simply tells the browser to access another page. Server. Transfer helps reduce server requests, keep the address bar URL unchanged, and allow you to pass querystring and form variables to another page (with a small flaw ).

Important: do not confuse the server. transfer and server. execute, server. execute Executes a page and returns the result. execute is very useful, but in ASP.. net, it is replaced by the fresher method, so ignore server. execute.

In addition

Execution Process

1. browser ASP file request-> server execution-> response. Redirect statement-> the server sends the address following response. Redirect to the client browser-> the browser requests a new address.

2. browser ASP file request-> server execution-> server. Transfer statements-> server redirection to new files

Switch object

1. response. Redirect can switch to any existing webpage.

2. server. Transfer can only switch to a webpage in the same directory or subdirectory.

Data Confidentiality

1. After response. Redirect, The address will change to the page address after the jump.

2. The address remains unchanged after server. Transfer, hiding the address of the new web page and the parameter values attached to the address. Data Confidentiality.

Transmitted data volume (parameters attached to the URL)

1. The data that response. Redirect can transmit is limited to 2 kb.

2. When the transmitted data exceeds 2 kb, you must use server. Transfer.

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.