Server. Transfer (Personal organization ),

Source: Internet
Author: User

Server. Transfer (Personal organization ),

Server. Transfer

This method is a little more complex than the previously described method, but it is particularly useful in inter-page value transfer, using this method, you can access the exposed value in the form of Object Attributes on another page. Of course, this method is used, you need to write additional code to create some attributes so that you can access it on another page. However, the benefits of this method are also obvious.

In general, Server. transfer is a 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.

Server. transfer Transfers the current ASPX page to the new ASPX page. The server executes the new page and outputs the new page through Context. handler to obtain the values, form data, and QueryString of various data types passed on the previous page. because the redirection is completed on the server side, the URL in the client browser will not change. When Server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to another ASPX page. However, the new ASPX page still uses the response stream created on the previous ASPX page.

Ps: Compare the differences between Server. Transfer and Response. Redirect.
(1) Server. transfer is completed on the server, so the URL in the client browser will not change; Response. redirect is completed by the client and a new page Processing request is submitted to the server, so the URL address in the client browser will change.
(2) The Server. Transfer is completed on the Server without the need for a request from the client. This reduces the number of requests sent from the client to the Server.
(3) 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.
(4) Server. transfer can upload values of the previous page to the new page; Response. redirect can only use parameters in the URL or the above four methods to upload various types of values to the new page.

Advantages: 1. Direct redirection on the server side, which is easy to use and reduces the number of requests sent from the client to the server.

2. Various data types and control values can be passed.

Disadvantages: 1. the URL address in the client browser is unchanged, which may cause unexpected problems on the new page. For example, if the source page and the target page are not in the same virtual directory or their sub-directories, images and hyperlinks using relative paths will lead to incorrect pointing.

Method of use: 1. In the code on the Source Page, use the Server. Transfer of the Page class to jump to another Page and pass the Page data: Server. Transfer ("B. aspx", "false ").

2. on the target page, use Context. handler to receive data: FormerPage formerPage = (FormerPage) Context. handler; then, use the attributes and methods of formerPage to obtain the value of the previous page, or directly use Context. items ["myParameter"]

Example: (1) a. aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="a.aspx.cs" Inherits="WebApplication.a" %><!DOCTYPE html>

(2) a. aspx. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace WebApplication {public partial class a: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} public string Name => TextBox1.Text. trim (). toString (); // It must be passed to B. aspx page value protected void button#click (object sender, EventArgs e) {Server. tra Nsfer ("B. aspx "); // Note: using this method, the address of the IE address bar will not change. // actually equivalent to. aspx tells ASP. NET Processing Engine. You can execute B for me. aspx, and then give the result to me. I will give it to the user. Therefore, the browser thinks it is A. aspx, and the server actually executes B. aspx for A. aspx }}}

(3) B. aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b.aspx.cs" Inherits="WebApplication.b" %><!DOCTYPE html>

(4) B. aspx. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace WebApplication {public partial class B: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {var a = (a) Context. handler; // use Context. the Handler property is used to obtain the reference of the previous page instance object. With this property, the value of var getvalue = a can be used to access the control of the previous page. name; Response. write (getvalue );}}}

 

 

Ps: This article is based on my understanding on the Internet. If your rights and interests are inadvertently violated, please contact me.

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.