Application of server. Transfer

Source: Internet
Author: User
Server. Transfer, response. Redirect differences
Differences between server. Transfer and response. Redirect
If you have read many industry magazines and ASP. net example, you will find that most people use response. redirect directs the user to another page, while others seem to prefer the mysterious server. transfer, so what is the difference between the two?
Response. Redirect simply sends a message to the browser, telling the browser to locate another page. You can use the following code to direct the user to another page:
Response. Redirect ("webform2.aspx ")
Or
Response. Redirect ("http://www.cnnas.com /")
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), the query string 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 query string 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 the query string and form variables to another page (with a small defect ).
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.

 

The following is an application instance of server. Transfer:

Webform1.aspx:

Private string _ name;
Public string name
{
Get
{
Return _ name;
}
Set
{
_ Name = value;
}
}

Private void button2_click (Object sender, system. eventargs E)
{
Name = "string from webform1 ";
Server. Transfer ("webform2.aspx ");
}

Webform2.aspx:

Private void page_load (Object sender, system. eventargs E)
{
Webform1 WF1;
WF1 = (webform1) Context. Handler;
Response. Write (wf1.name );
}

Another Application below: Passing values between two pages, or even calling the method of the previous page
ASP. NET Server. transfer () is a good way to pass values between two pages. When transfer is transferred from page a to page B, you can use context on page B. handler obtains an instance of a class on the page and calls each member object of A in B.

In the following example, webform1 and webform2 are created. The server. Transfer () method is used to demonstrate how to read the textbox, read attributes, pass values through context, and call webform1 in webform2:

Put a textbox1 and a button1 on webform1. The program is as follows:

Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox textbox1;
Protected system. Web. UI. webcontrols. Button button1;

Private void page_load (Object sender, system. eventargs E)
{
Context. Items. Add ("context", "context from form1 ");
}
Public String time
{
Get {return datetime. Now. tostring ();}
}
Public String testfun ()
{
Return "function of webform1 called ";
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}

Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx", true );
}

Place a literal1 control on webform2. The program is as follows:

Public class webform2: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Literal literal1;

Private void page_load (Object sender, system. eventargs E)
{
String strtxt = "";
Webform1 oform = (webform1) This. Context. Handler;
Strtxt + = "value of textbox:" + request. Form ["textbox1"] + "<br> ";
Strtxt + = "time property:" + oform. Time + "<br> ";
Strtxt + = "context string:" + context. items ["context"]. tostring () + "<br> ";
Strtxt + = oform. testfun () + "<br> ";
Literal1.text = strtxt;
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}

Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}

The second parameter of the transfer method indicates whether the form and querrystring values of the page are retained. You can set it to false, and the textbox1 value cannot be read in webform2.

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.