1. Server. Transfer and Response. Redirect, response. redirect

Source: Internet
Author: User

1. Server. Transfer and Response. Redirect, response. redirect

Today, when using ServerTransfer and Response. Redirect to locate the current page to refresh the page, we found some phenomena:

1. Use Response. Redirect to refresh the page, causing the data displayed on the current page to disappear:

Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {String Path; string connString = "server = .; database = ComInfo; integrated security = true "; SqlConnection conn = new SqlConnection (connString); conn. open (); String PassUserid = Request. queryString ["C_Id"]; // obtain the field value passed on the previous page. String strsql = "select E_Id, E_Name, E_Sex, E_Position, E_Organisation, E_Phone, e_Address from Emp where C_Id = '"+ PassUserid +"' "; SqlDataAdapter da = new SqlDataAdapter (strsql, conn); DataSet ds = new DataSet (); da. fill (ds); GridView1.DataSource = ds; GridView1.DataBind (); conn. close ();}}

On page A, add A column of HypLinkField in the GridView control, whose Text value is: details. Set the DataNavigateUrlFormatString attribute to "~ /B .. aspx "? C_Id "; enter the field name to be passed in the DataNavigateUrlField attribute: C_Id. Write the above Code in the. cs file Load function on page B. In this way, click "details" On page A to Go to page B. The values of the Emp table and C_Id are displayed on page B.

In this case, use Response. Redirect ("B .. aspx"); When refreshing, the displayed information disappears. However, Server. Transfer does not.

2. Using Response. Redirect to refresh this page will not cause the display information to disappear:

    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            String strConn = "server=.;database=ComInfo;integrated security=true;";            String PassUserid = Session["C_Id"].ToString();            SqlConnection conn = new SqlConnection(strConn);            conn.Open();            string strsql = "select E_Id,E_Name,E_Sex,E_Position,E_Organisation,E_Phone,E_Address from Emp where C_Id ='" + PassUserid + " '";            SqlDataAdapter da = new SqlDataAdapter(strsql, conn);            DataSet ds = new DataSet();            da.Fill(ds);            GridView1.DataSource = ds;            GridView1.DataBind();            conn.Close();        }    }

The method used to pass values between pages is Session. In A, use Session ["C_Id"] = strId to record the value of C_Id, which is obtained on page B. After refresh the page with Response. Redirect or server. transfer, the data will not disappear.

Analysis: The value passed to the B page in the first method. It is released after being refreshed through Response. redirect, so that the query statement cannot find the corresponding information. When you use the first method to transfer values between pages, pay attention to the jump statement used. Do not use the first method.

The following is a comparison between Server. Transfer and Response. Redirect.

1. Server. Transfer can only jump to the page specified by the local virtual directory, while Response. Redirect is very flexible;
2. Server. Transfer can easily pass the page parameters to the specified page;
3. After the Server. Transfer jumps to another page, the address displayed by the browser does not change because the redirection is completely performed on the Server. The browser does not know that the Server has executed a page change.
4. Server. Transfer can reduce client requests to the Server;
5. The Server. Transfer Method transfers the execution process from the current. aspx file to another. aspx page on the same Server. 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. Response. Redirect is the first time the client requests the Server to return status code 302 and a new URL. The client requests a new URL again, and the Server returns a new page, which is one more Response than Server. Transfer.


What is the difference between responseredirect and ServerTransfer?

The Server. Transfer Method transfers the execution process from the current ASPX file to another ASPX page on the same Server. 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.

If you use the Server. Transfer method to implement navigation between pages, the URL in the browser will not change because the redirection is completely performed on the Server side, and the browser does not know that the Server has executed a page transformation.

Response. Redirect is the first time the client requests the Server to return status code 302 and a new URL. The client requests a new URL again, and the Server returns a new page, which is one more Response than Server. Transfer.

Analyze the ServerTransfer method. What are the differences between the ServerExecute method and the ResponseRedirect method?

(1) Server. Transfer Method:
Server. Transfer ("m2.aspx"); // page redirection (executed on the Server ).
The server stops parsing this page, saves the data on this page and then redirects the page to m2.aspx,
Add the m2.aspx page to the front data and return the result to the browser. Note that the address of the browser has not changed.
Or m1.aspx,

(2) Server. Execute method:
Server. Execute ("m2.aspx ");
After the server saves this page to the previous data, It redirects the page to m2.aspx for execution,
Return to this page and continue execution. Merge the three results and return them to the browser.

All of the above are server-side page redirection, so there is no page change record in the browser (the displayed address will not change ).
Therefore, if you refresh this page, there may be some other unexpected situations.
This type of page redirection can complete other functions, such as accessing the server control on the previous page.

(3) Response. Redirect:
When the browser requests the aspx page, it encounters the Redirect (url) method,
It is equivalent to telling the browser that you need to access a page first, so the browser then sends a request to the server to access this page.
Relocation is performed through the browser, and an additional round-trip process is generated between the server and the browser.
When the network condition is not good, the two requests will be large.
Reduces the response speed of the application, or even occupies excessive bandwidth. After the conversion, the browser address is changed and displayed as asp2.aspx.

Summary:
When the network status is good, the Redirect (url) method is the most efficient !!
The Server. Transfer Method and Server. Execute method are the most flexible !!
The Server. Execute method occupies the most resources.
 

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.